趋近智
if 语句for 循环with 自动关闭文件from ... importself 参数说明finally 块:清理操作Python列表的一个重要特点是它们的可变性。与字符串或元组(我们很快就会介绍)不同,列表创建后,可以改变、添加或删除其中的元素。这种灵活性使得列表在存储程序运行过程中可能需要变化的数据集合时非常有用。
如果你知道要改变的元素的位置(索引),可以直接为该索引赋值新内容。请记住,列表索引从0开始。
# 一个颜色列表
colors = ["red", "green", "blue"]
print(f"Original list: {colors}")
# 将索引1处的元素(第二个元素)从“green”改为“yellow”
colors[1] = "yellow"
print(f"After changing index 1: {colors}")
# 使用负数索引改变最后一个元素
colors[-1] = "purple"
print(f"After changing index -1: {colors}")
这种直接赋值会用新值替换指定索引处的现有值。如果你使用的索引在列表中不存在,Python会引发 IndexError。
Python提供了几种向列表添加新元素的方法。
append()添加单个元素最常见的方法是使用 append() 方法。这会将元素添加到列表的末尾。
# 今日待办事项列表
tasks = ["email team", "review report"]
print(f"Tasks to do: {tasks}")
# 在末尾添加一个新任务
tasks.append("buy groceries")
print(f"After append: {tasks}")
append() 方法简单明了,并且在每次添加一个元素来增长列表时很有效率。
insert()如果你需要在非末尾的特定位置添加元素,请使用 insert() 方法。它接受两个参数:你希望插入元素的位置索引,以及元素本身。从该索引开始的现有元素都会向右移动一个位置。
# 在开头(索引0)插入一个高优先级任务
tasks.insert(0, "call client")
print(f"After insert at index 0: {tasks}")
# 在“buy groceries”之前(它现在在索引3)插入另一个任务
tasks.insert(3, "prepare meeting notes")
print(f"After insert at index 3: {tasks}")
对于大型列表,使用 insert() 可能不如 append() 高效,因为移动元素需要时间。
extend()要将另一个可迭代对象(如另一个列表、元组或字符串)中的所有元素添加到当前列表的末尾,请使用 extend() 方法。
# 从另一个列表添加更多任务
more_tasks = ["schedule follow-up", "submit expenses"]
tasks.extend(more_tasks)
print(f"After extend: {tasks}")
重要的是要注意 extend() 与 append() 的区别。如果你 append() 一个列表,整个列表会作为一个单独的元素添加。extend() 则会添加可迭代对象中的每个独立元素。
numbers = [1, 2, 3]
extra_numbers = [4, 5]
# 使用 extend(添加单个元素的正确方法)
numbers.extend(extra_numbers)
print(f"After extend: {numbers}") # 输出:[1, 2, 3, 4, 5]
# 重置 numbers 并尝试 append
numbers = [1, 2, 3]
numbers.append(extra_numbers)
print(f"After append: {numbers}") # 输出:[1, 2, 3, [4, 5]] - 注意嵌套列表!
正如存在添加元素的方法一样,也有几种删除元素的方法。
remove()如果你知道要删除的元素的值,但不知道它的索引,请使用 remove() 方法。它会在列表中查找该值的第一次出现并将其删除。
# 让我们回到颜色列表
colors = ["red", "yellow", "purple", "yellow"]
print(f"Current colors: {colors}")
# 删除第一个“yellow”
colors.remove("yellow")
print(f"After remove('yellow'): {colors}")
如果你要删除的值在列表中未找到,Python会引发 ValueError。
pop()如果你想根据元素的位置(索引)删除它,请使用 pop() 方法。这个方法还会返回被删除的元素,这会很有用。如果不指定索引,pop() 会删除并返回列表中的最后一个元素。
print(f"Current colors: {colors}") # 应该是 ['red', 'purple', 'yellow']
# 删除并获取索引1处的元素(“purple”)
removed_color = colors.pop(1)
print(f"Removed color: {removed_color}")
print(f"After pop(1): {colors}")
# 删除并获取最后一个元素(默认行为)
last_color = colors.pop()
print(f"Removed last color: {last_color}")
print(f"After pop(): {colors}")
使用无效索引调用 pop() 会导致 IndexError。
del 语句del 语句是使用索引从列表中删除元素或切片的一种更通用的方式。与 pop() 不同,del 不会返回被删除的值。
numbers = [10, 20, 30, 40, 50, 60]
print(f"Original numbers: {numbers}")
# 删除索引0处的元素
del numbers[0]
print(f"After del numbers[0]: {numbers}")
# 删除从索引2开始(不包括索引4)的元素
# 这会删除当前索引2和3处的元素(原来是30和40,现在是40和50)
del numbers[2:4]
print(f"After del numbers[2:4]: {numbers}")
# del 也可以删除整个列表变量,但这与清空列表不同
# del numbers
# print(numbers) # 这现在会导致 NameError
clear()如果你想从列表中删除所有元素,使其变为空列表,请使用 clear() 方法。
numbers = [10, 60] # 来自上一个示例
print(f"Numbers before clear: {numbers}")
numbers.clear()
print(f"Numbers after clear: {numbers}")
这会就地修改列表,使其变为空。列表变量仍然存在,只是它不包含任何元素了。
列表修改的可视化表示,从
['A', 'B', 'C']开始。每一步都显示了应用修改方法后的结果。
了解这些改变、添加和删除元素的方法,会为你有效管理使用Python列表的动态数据集合提供所需工具。请根据你是否知道元素的值或位置,以及是要添加/删除单个还是多个元素来选择最适合的方法。
这部分内容有帮助吗?
© 2026 ApX Machine Learning用心打造