Python列表的定义及使用方法是什么

这篇文章主要介绍“Python列表的定义及使用方法是什么”,在日常操作中,相信很多人在Python列表的定义及使用方法是什么问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”Python列表的定义及使用方法是什么”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!

    1、定义和使用列表

    Python中,列表是由一系列元素按照特定的顺序构成的数据结构,也就是说列表类型的变量可以存储多个数据,且可以重复。

    1.1 定义列表

    使用[]字面量语法定义变量,列表中的多个元素使用逗号,进行分割,

    示例代码如下:

    list1 = ["Hello", "一碗周", "你好"]
    list2 = [1, 2, 3, 4, 5]
    print(list1)  # ['Hello', '一碗周', '你好']
    print(list2)  # [1, 2, 3, 4,5]

    使用Python内置的list将其他序列编程列表,示例代码如下:

    list1 = list(range(10))
    list2 = list("hello")
    print(list1)  # [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
    print(list2)  # ['h', 'e', 'l', 'l', 'o']

    列表是一种可变的数据类型,也就是可以对列表的的元素可以进行修改,这与字符串有显著的差别,对字符串类型进行修改后,都会返回新的字符串

    2、 访问列表中的值

    如果访问列表中的某个值,使用下标索引来访问列表中的值,与字符串一样使用方括号的形式截取字符,示例代码如下:

    list1 = ["Hello", "一碗周", "你好"]
    # 列表的索引
    print(list1[1])  # 一碗周
    # 列表的切片
    print(list1[1:3])  # ['一碗周', '你好']
    2.1  列表的运算符

    表和字符串类型一样,同样支持拼接、重复、成员运算等操作,

    示例代码如下:

    list1 = ["Hello"]
    list2 = ["World"]
    list3 = [1, 2, 3, 4, 5]
    list4 = list(range(1, 6))
    
    list5 = list1 + list2  # ['Hello', 'World']
    print(list5)
    
    list6 = list1 * 3  # ['Hello', 'Hello', 'Hello']
    list7 = list3 * 2  # [1, 2, 3, 4, 5, 1, 2, 3, 4, 5]
    print(list6)
    print(list7)
    
    print("W" in list1)  # False
    print("W" in list2)  # False
    
    # 列表的比较运算
    # 两个列表比较相等性比的是对应索引位置上的元素是否相等
    print(list3 == list4)  # True
    list8 = list(range(1, 7))
    print(list3 == list8)  # False

    2.2 列表元素的遍历

    遍历列表同遍历字符串是一样的,示例代码如下:

    list1 = ["H", "e", "l", "l", "o"]
    # 方法1
    for index in range(len(list1)):
        print(list1[index])
    # 方法2
    for ch in list1:
        print(ch)

    3、列表的方法

    3.1 添加和删除元素

    直接上代码

    list1 = ["cute", "beautiful", "一碗周"]
    
    # append()在列表尾部添加元素
    list1.append("lovely")
    print(list1)  # ['cute', 'beautiful', '一碗周', 'lovely']
    
    # insert()在列表指定索引位置插入元素
    list1.insert(2, "prefect")
    print(list1)  # ['cute', 'beautiful', 'prefect', '一碗周', 'lovely']
    
    # remove()删除指定元素
    list1.remove("lovely")
    print(list1)  # ['cute', 'beautiful', 'prefect', '一碗周']
    
    # pop()删除指定索引位置的元素
    list1.pop(2)
    print(list1)  # ['cute', 'beautiful', '一碗周']
    
    # clear()清空列表中的元素
    list1.clear()
    print(list1)  # []

    Python中也可以使用del关键字对列表元素进行删除,类似于pop,示例代码↓

    list1 = ["cute", "beautiful", "甜甜"]
    
    del list1[1]
    print(list1) # ['cute', '甜甜']
    
    # 删除整个列表
    del list1
    print(list1) # NameError: name 'list1' is not defined

    3.2 元素位置和次数

    使用index()来查找元素的位置,使用count()来统计元素出现的次数

    list1 = ["beautiful", "cute", "beautiful",
             'prefect', "beautiful", "一碗周", 'lovely']
    # 查找"beautiful"第一次出现的位置
    print(list1.index("beautiful"))  # 0
    # 从第四个元素以后查找"beautiful"最近一次出现的位置
    print(list1.index("beautiful", 3))  # 4
    
    # 统计"beautiful"出现的次数
    print(list1.count("beautiful"))  # 3

    3.3 元素排序和反转

    使用sort()方法可以实现列表元素的排序,而reverse()方法可以实现元素的反转,示例代码↓

    list1 = ["cute", "beautiful", "一碗周"]
    list2 = list(range(10))
    
    # 排序
    list1.sort()
    print(list1)  # ['beautiful', 'cute', '一碗周']
    
    # 反转
    list1.reverse()
    print(list1)  # ['一碗周', 'cute', 'beautiful']
    
    list2.reverse()
    print(list2)  # [9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
    
    # 前面的操作原来的列表进行修改,如果不让原来的数据被破坏可以使用copy()备份一份
    list3 = list2.copy()
    list3.sort()
    print(list2)  # [9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
    print(list3)  # [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

    3.4 小结

    Python列表的定义及使用方法是什么

    Python列表的定义及使用方法是什么

    4、列表的生成式

    要求:为字符串123和字符串ABC创建一个笛卡尔积构成的列表,示例代码如下:

    原始方法:

    a = "123"
    b = "ABC"
    list1 = []
    for x in a:
         for y in b:
              list1.append(x + y)
    print(list1) # ['1A', '1B', '1C', '2A', '2B', '2C', '3A', '3B', '3C']

    生成列方法:

    a = "123"
    b = "ABC"
    list1 = [x + y for x in a for y in b]
    print(list1) # ['1A', '1B', '1C', '2A', '2B', '2C', '3A', '3B', '3C']

    这中方法不仅代码量少,而且性能上也要优于普通的for循环和append追加的方式

    5、嵌套的列表

    因为列表里面的变量可以存储多种数据类型,就出现了列表里面有列表的时候,称之为列表的嵌套,示例代码如下:

    list1 = [["cute", "beautiful", "一碗周"], "cute", "beautiful", "一碗周"]
    
    print(list1[0])  # ['cute', 'beautiful', '一碗周']
    print(list1[1])  # cute
    
    # 如果想要查看被嵌套的那个cute则需要使用多个索引值
    print(list1[0][0])  # cute

    不管嵌套多少都是同理的

    到此,关于“Python列表的定义及使用方法是什么”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注网站,小编会继续努力为大家带来更多实用的文章!