Python列表操作完全指南:定义、索引、切片、遍历与常用方法

📅 2026/7/19 4:18:59
Python列表操作完全指南:定义、索引、切片、遍历与常用方法
1. 定义列表在Python中列表List是一种有序、可变的数据结构可以存储任意类型的元素元素之间用逗号分隔整个列表用方括号[]括起来。# 定义空列表 empty_list [] 定义包含不同类型元素的列表 mixed_list [1, hello, 3.14, True] 使用list()构造函数 list_from_range list(range(5)) # [0, 1, 2, 3, 4] list_from_string list(Python) # [P, y, t, h, o, n]2. 索引访问列表中的每个元素都有一个索引下标从0开始从左向右递增也可以使用负数索引从右向左访问-1表示最后一个元素。fruits [apple, banana, cherry, date, elderberry] 正数索引 print(fruits[0]) # apple print(fruits[2]) # cherry 负数索引 print(fruits[-1]) # elderberry print(fruits[-3]) # cherry 索引越界会报错 print(fruits[10]) # IndexError: list index out of range3. 切片操作切片Slicing用于获取列表的子集语法为list[start:stop:step]其中start包含stop不包含step为步长。numbers [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] 基本切片 print(numbers[2:5]) # [2, 3, 4] print(numbers[:3]) # [0, 1, 2] # 从开头到索引2 print(numbers[5:]) # [5, 6, 7, 8, 9] # 从索引5到结尾 print(numbers[:]) # [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] # 复制整个列表 使用步长 print(numbers[::2]) # [0, 2, 4, 6, 8] # 每隔一个元素 print(numbers[1::2]) # [1, 3, 5, 7, 9] print(numbers[::-1]) # [9, 8, 7, 6, 5, 4, 3, 2, 1, 0] # 反转列表 负索引切片 print(numbers[-5:-2]) # [5, 6, 7] print(numbers[-3:]) # [7, 8, 9]4. 遍历列表Python提供了多种遍历列表的方式最常用的是for循环。colors [red, green, blue, yellow] 方法1直接遍历元素 for color in colors: print(color) 方法2遍历索引和元素使用enumerate for index, color in enumerate(colors): print(f索引{index}: {color}) 方法3使用range遍历索引 for i in range(len(colors)): print(fcolors[{i}] {colors[i]}) 方法4列表推导式创建新列表 uppercase_colors [color.upper() for color in colors] print(uppercase_colors) # [RED, GREEN, BLUE, YELLOW] 方法5while循环 i 0 while i len(colors): print(colors[i]) i 15. 常用方法Python列表提供了丰富的内置方法以下是最常用的方法5.1 添加元素my_list [1, 2, 3] append() - 在末尾添加单个元素 my_list.append(4) # [1, 2, 3, 4] extend() - 在末尾添加多个元素合并列表 my_list.extend([5, 6]) # [1, 2, 3, 4, 5, 6] insert() - 在指定位置插入元素 my_list.insert(2, 99) # [1, 2, 99, 3, 4, 5, 6]5.2 删除元素my_list [10, 20, 30, 40, 50, 30] remove() - 删除第一个匹配的元素 my_list.remove(30) # [10, 20, 40, 50, 30] pop() - 删除并返回指定位置的元素 removed my_list.pop(1) # removed20, my_list[10, 40, 50, 30] del语句 - 删除指定位置或切片 del my_list[0] # [40, 50, 30] del my_list[1:3] # [40] clear() - 清空列表 my_list.clear() # []5.3 查找和统计my_list [5, 2, 8, 2, 9, 2, 1] index() - 返回第一个匹配元素的索引 index my_list.index(2) # 1 count() - 统计元素出现次数 count my_list.count(2) # 3 in运算符 - 检查元素是否存在 has_8 8 in my_list # True has_10 10 in my_list # False5.4 排序和反转my_list [3, 1, 4, 1, 5, 9, 2] sort() - 原地排序修改原列表 my_list.sort() # [1, 1, 2, 3, 4, 5, 9] my_list.sort(reverseTrue) # [9, 5, 4, 3, 2, 1, 1] sorted() - 返回排序后的新列表不修改原列表 new_list sorted(my_list) # [1, 1, 2, 3, 4, 5, 9] reverse() - 原地反转 my_list.reverse() # [1, 1, 2, 3, 4, 5, 9] → [9, 5, 4, 3, 2, 1, 1]5.5 复制列表original [1, 2, [3, 4]] 浅拷贝 - 只复制第一层 shallow_copy original.copy() # 或 list(original) 或 original[:] shallow_copy[2][0] 99 # 会影响original[2][0] 深拷贝 - 复制所有嵌套层次 import copy deep_copy copy.deepcopy(original) deep_copy[2][0] 99 # 不会影响original5.6 其他实用方法# len() - 获取列表长度 length len([1, 2, 3]) # 3 max()/min() - 获取最大/最小值 max_value max([5, 2, 8, 1]) # 8 min_value min([5, 2, 8, 1]) # 1 sum() - 求和仅限数值列表 total sum([1, 2, 3, 4]) # 10 join() - 字符串列表连接 words [Hello, World, Python] sentence .join(words) # Hello World Python6. 总结Python列表是编程中最常用的数据结构之一掌握其基本操作对于高效编程至关重要定义使用方括号[]或list()构造函数索引从0开始支持负数索引切片[start:stop:step]语法灵活获取子集遍历for循环、enumerate、列表推导式等多种方式常用方法添加、删除、查找、排序、复制等丰富操作列表的可变性和丰富的方法使其成为处理有序数据集合的理想选择。在实际编程中应根据具体需求选择合适的操作方法并注意浅拷贝与深拷贝的区别。