• python之列表及操作相关知识


    python之列表及操作相关知识

    1、列表的格式

    列表(List)是一种有序和可更改的集合,允许重复的成员。在 Python 中,列表用方括号编写。
    如下变量 studentList 的类型为列表
    请添加图片描述

    比C语言的数组强大的地方在于列表中的元素可以是不同类型的
    请添加图片描述

    2、列表的打印

    示例

    studentList = ["Cindy", "Bob", "Alice", "David"]  # 创建列表
    print(studentList)
    
    • 1
    • 2

    运行结果:
    请添加图片描述

    可以通过引用索引号来访问列表项目
    示例

    studentList = ["Cindy", "Bob", "Alice", "David"]
    print(studentList[1])   # 打印列表的第 2 项
    
    • 1
    • 2

    运行结果:
    请添加图片描述

    负索引表示从末尾开始,-1 表示最后一个项目,-2 表示倒数第二个项目,依此类推
    示例

    studentList = ["Cindy", "Bob", "Alice", "David"]
    print(studentList[-1])   # 打印列表的最后一项
    
    • 1
    • 2

    运行结果:
    请添加图片描述

    可以通过指定范围的起点和终点来指定索引范围。指定范围后,返回值是包含指定项目的新列表。范围区间为左闭右开
    示例

    studentList = ["Cindy", "Bob", "Alice", "David"]
    print(studentList[1:3])   # 搜索将从索引 1(包括)开始,到索引 3(不包括)结束
    
    • 1
    • 2

    运行结果:
    请添加图片描述

    如果要从列表末尾开始搜索,可以使用负索引

    studentList = ["Cindy", "Bob", "Alice", "David"]
    print(studentList[-4:-1])   # 返回从索引 -4(包括)到索引 -1(不包括)的项目
    
    • 1
    • 2

    运行结果:
    请添加图片描述

    若要改特定项目的值,需引用索引号
    示例

    studentList = ["Cindy", "Bob", "Alice", "David"]
    studentList[2] = "Allen"    # 更改索引为 2 的项
    print(studentList)
    
    • 1
    • 2
    • 3

    运行结果:
    请添加图片描述

    3、列表的循环遍历

    3.1 使用for循环

    为了更有效率的输出列表的每个数据,可以使用 for 循环遍历列表项来完成
    示例

    studentList = ["Cindy", "Bob", "Alice", "David"]
    for i in studentList:
        print(i)
    
    • 1
    • 2
    • 3

    运行结果:
    请添加图片描述

    3.2 使用while循环

    为了更有效率的输出列表的每个数据,也可以使用 while 循环来完成
    示例

    studentList = ["Cindy", "Bob", "Alice", "David"]
    length = len(studentList)  # 使用 len() 方法确定列表中有多少项
    i = 0
    while i<length:
        print(studentList[i])
        i+=1
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    运行结果:
    请添加图片描述

    4、列表的相关操作

    4.1 添加元素(“增”)

    append( ) 方法
    append() 方法用于在列表的末尾添加一个元素
    语法如下:

    list.append(element)

    参数含义:
    element:必选参数。任何类型(字符串、数字、对象等)的元素

    示例

    studentList = ["Cindy", "Bob", "Alice", "David"]
    print(studentList)
    studentList.append("Allen") # 向 studentList 列表添加元素
    print(studentList)
    
    a = ["a","b"]
    studentList.append(a)       # 向 studentList 列表添加一个列表
    print(studentList)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    运行结果:
    请添加图片描述

    extend( ) 方法
    extend() 方法将指定的列表元素或任何可迭代的元素添加到当前列表的末尾
    语法如下:

    list.extend(iterable)

    参数含义:
    iterable:必选参数。任何可迭代对象(列表、集合、元组等)

    示例

    # 将列表2添加到列表1的时候,使用append( )把列表2当作一个元素添加到列表1中
    # 若要把添加的列表2中的每一个元素都添加到列表1中使用extend( )
    
    studentList = ["Cindy", "Bob", "Alice", "David"]
    print(studentList)
    a = ["a","b"]
    studentList.extend(a)       # 把 a 中的元素添加到 studentList 列表中
    print(studentList)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    运行结果:
    请添加图片描述

    insert( ) 方法
    insert() 方法在指定位置添加元素
    语法如下:

    list.insert(position, element)

    参数含义:
    position:必选参数。数字,指定在哪个位置添加元素。
    element:必选参数。元素,任何类型(字符串、数字、对象等)

    示例

    # insert( )添加元素 自己设置添加到列表的位置
    studentList = ["Cindy", "Bob", "Alice", "David"]
    print(studentList)
    studentList.insert(2,"Allen")       # 把值 "Allen" 作为第三个元素插入 studentList 列表
    print(studentList)
    
    • 1
    • 2
    • 3
    • 4
    • 5

    运行结果:
    请添加图片描述

    4.2 修改元素(“改”)

    修改元素时,要通过下标来确定要修改的是哪个元素,然后才能进行修改
    示例

    studentList = ["Cindy", "Bob", "Alice", "David"]
    print(studentList)
    studentList[0] = "Allen"
    print(studentList)
    
    • 1
    • 2
    • 3
    • 4

    运行结果:
    请添加图片描述

    4.3 查找元素(“查”)

    查找就是看指定的元素是否存在

    关键字 in , not in
    • in(存在):如果存在,结果为True,否则为False
    • not in(不存在):如果不存在,结果为True,否则为False

    示例

    # 待查找的列表
    studentList = ["Cindy", "Bob", "Alice", "David"]
    
    # 获取用户要查找的元素
    findWhat = input("请输入你要查找的姓名:")
    
    # 查找是否存在
    if findWhat in studentList:
        print("找到咯")
    else:
        print("没找到")
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    运行结果:
    请添加图片描述
    in 和 not in 用法相同,区别在于 not in 判断的是不存在

    index( ) 方法
    index() 方法返回指定值首次出现的索引位置,如果没有找到对象则抛出异常,与字符串中的用法相同
    语法如下:

    list.index(value, start, end)

    参数含义:
    value:必选参数。指定要检索的字符串
    start:可选参数。开始查找的索引,默认为0
    end:可选参数。结束查找的索引,默认为字符串的长度

    注意:index()函数在查找时,start处的字符将会被检查,而end是结束检索的索引,所以end索引处的字符不会参与检查

    示例

    aList = ["apple", 123, "banana", 456]
    print(aList.index(123))   # 查找 123 的索引位置
    print(aList.index("banana",1,4))  # 在索引1~3中查找 "banana" 的索引位置
    
    
    • 1
    • 2
    • 3
    • 4

    运行结果:
    请添加图片描述

    count( ) 方法
    count() 方法用于统计某个元素在列表中出现的次数,之前在讲字符串操作的时候也有该方法,但区别在于用于字符串可以指定查找的起始和结束位置,但是用于列表时就不可以了
    语法如下:

    list.count(value)

    参数含义:
    value:必选参数。任何类型(字符串、数字、列表、元组等)要搜索的值

    示例

    aList = [1,2,3,2,5,7,2,4,9,3]
    print(aList.count(2))  # 返回数值 2 在列表中出现的次数
    
    • 1
    • 2

    运行结果:
    请添加图片描述

    4.4 删除元素(“删”)

    关键字 del
    del 关键字可以根据下标删除列表的元素

    示例

    aList = ["apple", 123, "banana", 456]
    del aList[0]  # 删除指定的索引元素
    print(aList)
    
    del aList     # 也能完整地删除列表
    
    • 1
    • 2
    • 3
    • 4
    • 5

    运行结果:
    请添加图片描述

    pop( ) 方法
    pop() 删除指定位置的元素,并且返回被删除的元素
    语法如下:

    list.pop(pos)

    参数含义:
    pos:可选。数字,指定需要删除元素的位置。默认值 -1,删除最后一个元素,返回最后的项目

    示例

    aList = ["apple", 123, "banana", 456]
    x = aList.pop()  # 默认值为-1,返回被删除的元素
    print(x)
    print(aList)
    
    • 1
    • 2
    • 3
    • 4

    运行结果:
    请添加图片描述

    remove( ) 方法
    remove()根据元素的值进行删除,删除具有指定值的项目
    语法如下:

    list.remove(element)

    参数含义:
    element:必选参数。需要删除的任何类型(字符串、数字、列表等)的元素

    示例

    aList = ["apple", 123, "banana", 456]
    aList.remove(123)  # 删除列表的 123 元素
    print(aList)
    
    • 1
    • 2
    • 3

    运行结果:
    请添加图片描述

    4.5 排序

    sort( ) 方法
    默认情况下,sort() 方法对列表进行升序排序。还可以让函数来决定排序标准
    语法如下:

    list.sort(reverse=True|False, key=myFunc)

    参数含义:
    reverse:可选参数。reverse=True 将对列表进行降序排序。默认是 reverse=False
    key:可选参数。指定排序标准的函数

    示例

    studentList = ["Cindy", "Bob", "Alice", "David"]
    studentList.sort()        # 以字母顺序对列表进行升序排序
    print(studentList)
    
    aList = [1,5,3,4,7]
    aList.sort(reverse=True)  # 对列表进行降序排序
    print(aList)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    运行结果:
    请添加图片描述

    reverse( ) 方法
    reverse() 方法反转元素的排序顺序
    语法如下:

    list.reverse()

    无参数

    示例

    studentList = ["Cindy", "Bob", "Alice", "David"]
    studentList.reverse()   # 逆置列表的顺序
    print(studentList)
    
    aList = [1,5,3,4,7]
    aList.reverse()         # 逆置列表的顺序
    print(aList)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    运行结果:
    请添加图片描述

  • 相关阅读:
    【手写系列】手把手教你如何实现 列表转树-树转列表
    Latex 报错:The font cannot be found.
    解决跨境电商平台账号无法访问的常见问题
    单调栈题目:找出最具竞争力的子序列
    Elasticsearch 8.X 防止 Mapping “爆炸”的三种方案
    2022年最新《谷粒学院开发教程》:6 - 整合SpringCloud
    MongoDB数据库的安装和使用
    Spring Boot面试系列-01
    Redis02-持久化策略
    【Java】三种方案实现 Redis 分布式锁
  • 原文地址:https://blog.csdn.net/qq_45158642/article/details/127564240