Python小记(六):列表的定义和使用

941 阅读5分钟

小知识,大挑战!本文正在参与“程序员必备小知识”创作活动。

大家好,我是一碗周,一个不想被喝(内卷)的前端。如果写的文章有幸可以得到你的青睐,万分有幸~

定义和使用列表

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

定义列表

  1. 使用[]字面量语法定义变量,列表中的多个元素使用逗号,进行分割,示例代码如下:

    list1 = ["Hello", "一碗周", "你好"]
    list2 = [1, 2, 3, 4, 5]
    print(list1)  # ['Hello', '一碗周', '你好']
    print(list2)  # [1, 2, 3, 4,5]
    
  2. 使用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']
    

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

访问列表中的值

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

list1 = ["Hello", "一碗周", "你好"]
# 列表的索引
print(list1[1])  # 一碗周
# 列表的切片
print(list1[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

列表元素的遍历

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

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

列表的方法

添加和删除元素

直接上代码

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

元素位置和次数

使用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

元素排序和反转

使用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]

总结

方法描述参数
append()在列表末尾添加新的对象添加到列表末尾的对象。
insert()用于将指定对象插入列表的指定位置index -- 对象obj需要插入的索引位置。
obj -- 要插入列表中的对象。
remove()函数用于移除列表中某个值的第一个匹配项。列表中要移除的对象。
pop()函数用于移除列表中的一个元素(默认最后一个元素)可选参数,要移除列表元素的索引值,不能超过列表总长度,默认为 index=-1,删除最后一个列表值。
clear()函数用于清空列表
index()用于从列表中找出某个值第一个匹配项的索引位置。x-- 查找的对象。
start-- 可选,查找的起始位置。
end-- 可选,查找的结束位置。
count()用于统计某个元素在列表中出现的次数。列表中统计的对象。
sort()用于对原列表进行排序key -- 主要是用来进行比较的元素
reverse -- 排序规则,reverse = True 降序, reverse = False 升序(默认)
reverse()用于反向列表中元素
copy()用于复制列表

列表的生成式

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

嵌套的列表

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

list1 = [["cute", "beautiful", "一碗周"], "cute", "beautiful", "一碗周"]

print(list1[0])  # ['cute', 'beautiful', '一碗周']
print(list1[1])  # cute

# 如果想要查看被嵌套的那个cute则需要使用多个索引值
print(list1[0][0])  # cute

不管嵌套多少都是同理的