Python列表操作全攻略
Python 列表(List)的全面使用指南
Python 列表是一种有序、可变的数据结构,能够存储不同类型的元素。列表是 Python 中最常用的数据结构之一,广泛应用于数据处理、算法实现和日常编程任务。
创建列表
列表可以通过方括号 [] 直接创建,也可以使用 list() 构造函数。列表中的元素可以是任意数据类型,包括数字、字符串、布尔值,甚至其他列表。
# 直接创建列表
numbers = [1, 2, 3, 4, 5]
fruits = ['apple', 'banana', 'cherry']
mixed = [1, 'apple', True, 3.14]
# 使用 list() 构造函数
empty_list = list()
another_list = list(range(5))
访问列表元素
列表中的元素可以通过索引访问,索引从 0 开始。负数索引表示从列表末尾开始计数。
fruits = ['apple', 'banana', 'cherry']
first_fruit = fruits[0] # 'apple'
last_fruit = fruits[-1] # 'cherry'
修改列表
列表是可变的,可以通过索引直接修改元素值。
fruits = ['apple', 'banana', 'cherry']
fruits[1] = 'blueberry'
print(fruits) # ['apple', 'blueberry', 'cherry']
列表切片
切片操作可以获取列表的子集,语法为 list[start:end:step]。
numbers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
first_three = numbers[:3] # [0, 1, 2]
even_numbers = numbers[::2] # [0, 2, 4, 6, 8]
reverse_list = numbers[::-1] # [9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
列表常用方法
Python 提供了丰富的列表方法,用于添加、删除、查找和排序元素。
添加元素
append(): 在列表末尾添加单个元素extend(): 在列表末尾添加多个元素insert(): 在指定位置插入元素
fruits = ['apple', 'banana']
fruits.append('cherry') # ['apple', 'banana', 'cherry']
fruits.extend(['date', 'elderberry']) # ['apple', 'banana', 'cherry', 'date', 'elderberry']
fruits.insert(1, 'blueberry') # ['apple', 'blueberry', 'banana', 'cherry', 'date', 'elderberry']
删除元素
remove(): 删除第一个匹配的元素pop(): 删除并返回指定位置的元素(默认最后一个)clear(): 清空列表
fruits = ['apple', 'banana', 'cherry']
fruits.remove('banana') # ['apple', 'cherry']
last_fruit = fruits.pop() # 'cherry', fruits现在是['apple']
fruits.clear() # []
查找和排序
index(): 返回元素的索引count(): 统计元素出现的次数sort(): 原地排序列表reverse(): 原地反转列表
numbers = [3, 1, 4, 1, 5, 9, 2]
numbers.index(5) # 4
numbers.count(1) # 2
numbers.sort() # [1, 1, 2, 3, 4, 5, 9]
numbers.reverse() # [9, 5, 4, 3, 2, 1, 1]
列表推导式
列表推导式提供了一种简洁的方式来创建列表,通常比传统的循环更高效。
# 创建平方数列表
squares = [x**2 for x in range(10)]
# [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
# 带条件的列表推导式
even_squares = [x**2 for x in range(10) if x % 2 == 0]
# [0, 4, 16, 36, 64]
列表复制
列表复制需要注意浅拷贝和深拷贝的区别,特别是当列表包含可变对象时。
# 浅拷贝
original = [[1, 2], [3, 4]]
shallow_copy = original.copy()
shallow_copy[0][0] = 99
print(original) # [[99, 2], [3, 4]] 原始列表也被修改
# 深拷贝
import copy
deep_copy = copy.deepcopy(original)
deep_copy[0][0] = 100
print(original) # [[99, 2], [3, 4]] 原始列表不受影响
性能考虑
列表操作的时间复杂度各不相同,合理选择操作可以提高程序效率:
- 索引访问和赋值:O(1)
append()和pop(): O(1)- 插入和删除中间元素:O(n)
- 查找元素:O(n)
- 切片:O(k)(k是切片长度)
对于频繁插入和删除的场景,可以考虑使用 collections.deque,它在两端操作的时间复杂度都是 O(1)。
高级应用
列表解包
first, *middle, last = [1, 2, 3, 4, 5]
# first = 1, middle = [2, 3, 4], last = 5
zip函数
names = ['Alice', 'Bob', 'Charlie']
scores = [85, 90, 78]
for name, score in zip(names, scores):
print(f"{name}: {score}")
filter和map
numbers = [1, 2, 3, 4, 5]
squared = list(map(lambda x: x**2, numbers)) # [1, 4, 9, 16, 25]
evens = list(filter(lambda x: x % 2 == 0, numbers)) # [2, 4]
掌握这些列表操作技巧,可以显著提高 Python 编程的效率和代码质量。
BbS.okane469.info/PoSt/1121_260013.HtM
BbS.okane470.info/PoSt/1121_576661.HtM
BbS.okane471.info/PoSt/1121_423657.HtM
BbS.okane472.info/PoSt/1121_780449.HtM
BbS.okane473.info/PoSt/1121_229718.HtM
BbS.okane474.info/PoSt/1121_507490.HtM
BbS.okane475.info/PoSt/1121_236762.HtM
BbS.okane476.info/PoSt/1121_554674.HtM
BbS.okane477.info/PoSt/1121_997183.HtM
BbS.okane478.info/PoSt/1121_025017.HtM
BbS.okane469.info/PoSt/1121_585393.HtM
BbS.okane470.info/PoSt/1121_264542.HtM
BbS.okane471.info/PoSt/1121_729804.HtM
BbS.okane472.info/PoSt/1121_929129.HtM
BbS.okane473.info/PoSt/1121_116536.HtM
BbS.okane474.info/PoSt/1121_079366.HtM
BbS.okane475.info/PoSt/1121_871212.HtM
BbS.okane476.info/PoSt/1121_247054.HtM
BbS.okane477.info/PoSt/1121_630606.HtM
BbS.okane478.info/PoSt/1121_389385.HtM
BbS.okane469.info/PoSt/1121_290153.HtM
BbS.okane470.info/PoSt/1121_641172.HtM
BbS.okane471.info/PoSt/1121_392675.HtM
BbS.okane472.info/PoSt/1121_747567.HtM
BbS.okane473.info/PoSt/1121_653886.HtM
BbS.okane474.info/PoSt/1121_802810.HtM
BbS.okane475.info/PoSt/1121_253075.HtM
BbS.okane476.info/PoSt/1121_126936.HtM
BbS.okane477.info/PoSt/1121_805132.HtM
BbS.okane478.info/PoSt/1121_797023.HtM
BbS.okane469.info/PoSt/1121_775899.HtM
BbS.okane470.info/PoSt/1121_264590.HtM
BbS.okane471.info/PoSt/1121_443820.HtM
BbS.okane472.info/PoSt/1121_220093.HtM
BbS.okane473.info/PoSt/1121_867044.HtM
BbS.okane474.info/PoSt/1121_084529.HtM
BbS.okane475.info/PoSt/1121_492810.HtM
BbS.okane476.info/PoSt/1121_224990.HtM
BbS.okane477.info/PoSt/1121_159876.HtM
BbS.okane478.info/PoSt/1121_901838.HtM
BbS.okane469.info/PoSt/1121_012975.HtM
BbS.okane470.info/PoSt/1121_057195.HtM
BbS.okane471.info/PoSt/1121_846217.HtM
BbS.okane472.info/PoSt/1121_793256.HtM
BbS.okane473.info/PoSt/1121_965286.HtM
BbS.okane474.info/PoSt/1121_315338.HtM
BbS.okane475.info/PoSt/1121_744044.HtM
BbS.okane476.info/PoSt/1121_226232.HtM
BbS.okane477.info/PoSt/1121_718566.HtM
BbS.okane478.info/PoSt/1121_329511.HtM
BbS.okane469.info/PoSt/1121_094113.HtM
BbS.okane470.info/PoSt/1121_122788.HtM
BbS.okane471.info/PoSt/1121_656215.HtM
BbS.okane472.info/PoSt/1121_951702.HtM
BbS.okane473.info/PoSt/1121_213698.HtM
BbS.okane474.info/PoSt/1121_935251.HtM
BbS.okane475.info/PoSt/1121_479525.HtM
BbS.okane476.info/PoSt/1121_872117.HtM
BbS.okane477.info/PoSt/1121_377040.HtM
BbS.okane478.info/PoSt/1121_367025.HtM
BbS.okane469.info/PoSt/1121_905895.HtM
BbS.okane470.info/PoSt/1121_912129.HtM
BbS.okane471.info/PoSt/1121_623663.HtM
BbS.okane472.info/PoSt/1121_907987.HtM
BbS.okane473.info/PoSt/1121_636364.HtM
BbS.okane474.info/PoSt/1121_576353.HtM
BbS.okane475.info/PoSt/1121_140205.HtM
BbS.okane476.info/PoSt/1121_805981.HtM
BbS.okane477.info/PoSt/1121_619799.HtM
BbS.okane478.info/PoSt/1121_599383.HtM
BbS.okane469.info/PoSt/1121_478215.HtM
BbS.okane470.info/PoSt/1121_763289.HtM
BbS.okane471.info/PoSt/1121_146419.HtM
BbS.okane472.info/PoSt/1121_583473.HtM
BbS.okane473.info/PoSt/1121_180421.HtM
BbS.okane474.info/PoSt/1121_525676.HtM
BbS.okane475.info/PoSt/1121_827552.HtM
BbS.okane476.info/PoSt/1121_509898.HtM
BbS.okane477.info/PoSt/1121_279791.HtM
BbS.okane478.info/PoSt/1121_509444.HtM

