Python3(9) 列表
在 Python 编程中,列表(List)是一种极为重要且灵活的数据结构,它可以存储任意类型的数据,并且支持动态地添加、删除和修改元素。掌握列表的使用方法是编写高效 Python 代码的基础。本文将结合详细的代码示例,对 Python3 列表的相关知识进行系统梳理。
一、列表的创建
1.1 使用方括号创建
最常见的创建方式是使用方括号[]
,将元素用逗号分隔。元素可以是同一类型,也可以是混合类型。
# 整数列表
num_list = [1, 2, 3, 4, 5]
# 字符串列表
str_list = ["apple", "banana", "cherry"]
# 混合类型列表
mixed_list = [1, "hello", True, 3.14]print(num_list)
print(str_list)
print(mixed_list)
1.2 使用list()
函数创建
list()
函数可以将其他可迭代对象(如元组、字符串)转换为列表。
# 将元组转换为列表
tuple_data = (10, 20, 30)
new_list = list(tuple_data)
print(new_list)# 将字符串转换为列表
string_data = "python"
char_list = list(string_data)
print(char_list)
1.3 使用列表生成式创建
列表生成式提供了一种简洁的语法,用于快速生成列表。
# 生成1到10的平方数列表
squares = [x ** 2 for x in range(1, 11)]
print(squares)# 从列表中筛选出偶数
nums = [1, 2, 3, 4, 5, 6]
even_nums = [x for x in nums if x % 2 == 0]
print(even_nums)
二、列表的访问
2.1 索引访问
列表的索引从0
开始,也支持负数索引(-1
表示最后一个元素)。
fruits = ["apple", "banana", "cherry", "date"]
print(fruits[0]) # 输出: apple
print(fruits[-1]) # 输出: date
2.2 切片操作
切片用于获取列表的子序列,语法为list[start:stop:step]
(start
包含,stop
不包含,step
为步长)。
nums = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
print(nums[2:5]) # 输出: [3, 4, 5]
print(nums[::2]) # 输出: [1, 3, 5, 7, 9]
print(nums[::-1]) # 输出: [10, 9, 8, 7, 6, 5, 4, 3, 2, 1]
三、列表的修改
3.1 修改单个元素
通过索引直接赋值即可修改元素。
fruits = ["apple", "banana", "cherry"]
fruits[1] = "orange"
print(fruits) # 输出: ['apple', 'orange', 'cherry']
3.2 修改多个元素
使用切片替换多个元素。
nums = [1, 2, 3, 4, 5]
nums[1:3] = [20, 30, 40]
print(nums) # 输出: [1, 20, 30, 40, 5]
四、列表的元素添加
4.1 append()
方法
在列表末尾添加单个元素。
fruits = ["apple", "banana"]
fruits.append("cherry")
print(fruits) # 输出: ['apple', 'banana', 'cherry']
4.2 extend()
方法
将另一个可迭代对象的元素添加到列表末尾。
list1 = [1, 2, 3]
list2 = [4, 5, 6]
list1.extend(list2)
print(list1) # 输出: [1, 2, 3, 4, 5, 6]
4.3 insert()
方法
在指定位置插入元素。
fruits = ["apple", "cherry"]
fruits.insert(1, "banana")
print(fruits) # 输出: ['apple', 'banana', 'cherry']
五、列表的元素删除
5.1 del
语句
根据索引删除元素。
nums = [1, 2, 3, 4, 5]
del nums[2]
print(nums) # 输出: [1, 2, 4, 5]
5.2 pop()
方法
移除并返回指定位置的元素(默认删除最后一个)。
fruits = ["apple", "banana", "cherry"]
removed_fruit = fruits.pop(1)
print(removed_fruit) # 输出: banana
print(fruits) # 输出: ['apple', 'cherry']
5.3 remove()
方法
移除列表中第一个匹配的元素。
fruits = ["apple", "banana", "cherry", "banana"]
fruits.remove("banana")
print(fruits) # 输出: ['apple', 'cherry', 'banana']
六、列表的其他操作
6.1 列表长度
使用len()
函数获取列表元素个数。
fruits = ["apple", "banana", "cherry"]
print(len(fruits)) # 输出: 3
6.2 列表拼接
使用+
运算符或extend()
方法合并列表。
list_a = [1, 2, 3]
list_b = [4, 5, 6]
result1 = list_a + list_b
print(result1) # 输出: [1, 2, 3, 4, 5, 6]list_a.extend(list_b)
print(list_a) # 输出: [1, 2, 3, 4, 5, 6]
6.3 列表重复
使用*
运算符重复列表。
nums = [1, 2]
new_nums = nums * 3
print(new_nums) # 输出: [1, 2, 1, 2, 1, 2]
6.4 成员检查
使用in
关键字判断元素是否在列表中。
fruits = ["apple", "banana", "cherry"]
print("banana" in fruits) # 输出: True
print("grape" in fruits) # 输出: False
6.5 列表迭代
通过for
循环遍历列表。
fruits = ["apple", "banana", "cherry"]
for fruit in fruits:print(fruit)
6.6 列表排序
使用sort()
方法对列表进行排序(默认升序),或使用sorted()
函数生成排序后的新列表。
nums = [5, 3, 1, 4, 2]
nums.sort()
print(nums) # 输出: [1, 2, 3, 4, 5]nums = [5, 3, 1, 4, 2]
sorted_nums = sorted(nums, reverse=True)
print(sorted_nums) # 输出: [5, 4, 3, 2, 1]
6.7 列表反转
使用reverse()
方法原地反转列表,或使用切片[::-1]
生成反转后的新列表。
fruits = ["apple", "banana", "cherry"]
fruits.reverse()
print(fruits) # 输出: ['cherry', 'banana', 'apple']fruits = ["apple", "banana", "cherry"]
new_fruits = fruits[::-1]
print(new_fruits) # 输出: ['cherry', 'banana', 'apple']
七、列表的高级特性
7.1 嵌套列表
列表中包含其他列表,用于表示多维数据。
matrix = [[1, 2, 3],[4, 5, 6],[7, 8, 9]
]
print(matrix[0][1]) # 输出: 2
7.2 列表函数与方法
函数 / 方法 | 描述 |
---|---|
len(list) | 返回列表长度 |
max(list) | 返回列表中的最大值 |
min(list) | 返回列表中的最小值 |
list.count(x) | 返回元素x 在列表中出现的次数 |
list.index(x) | 返回元素x 第一次出现的索引 |
list.copy() | 返回列表的浅拷贝 |
list.clear() | 清空列表 |
八、总结
Python3 列表作为核心数据结构,在数据处理和算法实现中发挥着重要作用。通过掌握列表的创建、访问、修改、增删元素以及各种操作方法,能够灵活应对不同的编程需求。建议通过大量练习,加深对列表特性的理解,从而提升 Python 编程能力。