首页后端开发PythonPython中怎样定义和使用列表,列表的方法有什么?

Python中怎样定义和使用列表,列表的方法有什么?

时间2024-03-23 17:28:03发布访客分类Python浏览1489
导读:这篇文章给大家分享的是Python列表的相关内容,下文将介绍Python列表的定义和使用、列表的方法、列表的生成式、嵌套的列表等等,介绍非常详细,感兴趣的朋友接下来一起跟随小编看看吧。 1、定义和使用列表 在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']
    
    

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

    1.2 访问列表中的值

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

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

    1.3列表的运算符

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

    示例代码如下:

    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
    
    

    1.4 列表元素的遍历

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

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

    2、列表的方法

    2.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
    
    

    2.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
    
    

    2.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]
    
    

    2.4 小结

    3、列表的生成式

    要求:为字符串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追加的方式

    4、嵌套的列表

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

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

    不管嵌套多少都是同理的

    关于Python列表的定义和使用、列表的方法等等这些就介绍到这,上述示例具有一定的借鉴价值,感兴趣的朋友可以参考,希望能对大家有帮助,想要了解更多大家可以关注其它的相关文章。

    文本转载自脚本之家

    声明:本文内容由网友自发贡献,本站不承担相应法律责任。对本内容有异议或投诉,请联系2913721942#qq.com核实处理,我们将尽快回复您,谢谢合作!


    若转载请注明出处: Python中怎样定义和使用列表,列表的方法有什么?
    本文地址: https://pptw.com/jishu/651489.html
    MySQL数据库怎么实现主键自动增长 python中怎样进行数据类型转换,有什么方法?

    游客 回复需填写必要信息