本文目录导读:

我来为你介绍几种Python列表插入数据的实现方法。
使用 append() 方法(末尾添加)
# 在列表末尾添加单个元素
fruits = ['苹果', '香蕉']
fruits.append('橘子')
print(fruits) # 输出: ['苹果', '香蕉', '橘子']
# 添加多个元素(作为整个对象)
numbers = [1, 2, 3]
numbers.append([4, 5])
print(numbers) # 输出: [1, 2, 3, [4, 5]]
使用 insert() 方法(指定位置插入)
# 在指定索引位置插入单个元素 colors = ['红', '蓝', '绿'] colors.insert(1, '黄') # 在索引1位置插入'黄' print(colors) # 输出: ['红', '黄', '蓝', '绿'] # 在开头插入 numbers = [2, 3, 4] numbers.insert(0, 1) print(numbers) # 输出: [1, 2, 3, 4] # 超出列表长度时,会添加到末尾 letters = ['a', 'b'] letters.insert(100, 'c') # 索引超出长度,添加到末尾 print(letters) # 输出: ['a', 'b', 'c']
使用 extend() 方法(合并列表)
# 将另一个列表的所有元素添加到末尾
list1 = [1, 2, 3]
list2 = [4, 5, 6]
list1.extend(list2)
print(list1) # 输出: [1, 2, 3, 4, 5, 6]
# 也可以添加其他可迭代对象
numbers = [1, 2]
numbers.extend("hi") # 字符串也是可迭代的
print(numbers) # 输出: [1, 2, 'h', 'i']
使用切片赋值
# 替换或插入多个元素 nums = [1, 2, 5, 6] nums[2:2] = [3, 4] # 在索引2处插入3,4 print(nums) # 输出: [1, 2, 3, 4, 5, 6] # 替换指定范围的元素 colors = ['红', '绿', '蓝'] colors[1:2] = ['黄', '紫'] # 替换索引1的元素 print(colors) # 输出: ['红', '黄', '紫', '蓝']
使用 运算符合并列表
# 创建新列表,不修改原列表 list_a = [1, 2] list_b = [3, 4] result = list_a + list_b print(result) # 输出: [1, 2, 3, 4] print(list_a) # 原列表不变: [1, 2] # 在指定位置插入(需要切片) nums = [1, 4, 5] nums = nums[:1] + [2, 3] + nums[1:] print(nums) # 输出: [1, 2, 3, 4, 5]
实战案例:学生成绩管理系统
# 创建一个学生成绩管理系统
scores = []
def add_student(name, score):
"""添加学生成绩到列表末尾"""
student = {'name': name, 'score': score}
scores.append(student)
print(f"已添加学生:{name},成绩:{score}")
def insert_student(index, name, score):
"""在指定位置插入学生"""
if index < 0 or index > len(scores):
print("索引超出范围!")
return
student = {'name': name, 'score': score}
scores.insert(index, student)
print(f"已在位置{index}插入学生:{name}")
def merge_students(new_list):
"""合并另一个学生列表"""
scores.extend(new_list)
print(f"已合并{len(new_list)}个学生")
def batch_insert(index, student_list):
"""批量插入学生"""
scores[index:index] = student_list
print(f"已批量插入{len(student_list)}个学生")
# 测试
add_student("张三", 85)
add_student("李四", 92)
insert_student(1, "王五", 78) # 在张三和李四之间插入
print("\n当前学生列表:")
for i, s in enumerate(scores):
print(f"{i}: {s['name']} - {s['score']}分")
# 批量插入
new_students = [
{'name': '赵六', 'score': 95},
{'name': '钱七', 'score': 88}
]
batch_insert(2, new_students)
print("\n更新后的学生列表:")
for i, s in enumerate(scores):
print(f"{i}: {s['name']} - {s['score']}分")
| 方法 | 用途 | 是否修改原列表 | 性能特点 |
|---|---|---|---|
append() |
末尾添加单个元素 | 是 | O(1) |
insert() |
指定位置插入 | 是 | O(n) |
extend() |
合并可迭代对象 | 是 | O(k) |
| 切片赋值 | 批量插入/替换 | 是 | O(n+k) |
| 运算符 | 合并列表 | 否(返回新列表) | O(n+m) |
选择建议:
- 末尾添加:使用
append() - 指定位置插入:使用
insert() - 合并列表:使用
extend()或 运算符 - 批量插入:使用切片赋值
希望这些案例能帮助你掌握Python列表插入数据的方法!
版权声明:NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
CBA直播
CBA直播
CBA直播
CBA直播
CBA直播
CBA直播
CBA直播
CBA直播
CBA直播
CBA直播
CBA直播
CBA直播
CBA直播
CBA直播
CBA直播
CBA直播
CBA直播
CBA直播
足球直播
足球直播
足球直播
足球直播
足球直播
足球直播
足球直播
足球直播
足球直播
足球直播
足球直播
足球直播
足球直播
足球直播
足球直播
足球直播
足球直播
足球直播
英超直播
英超直播
英超直播
英超直播
英超直播
英超直播
英超直播
英超直播
英超直播
英超直播
英超直播
英超直播
英超直播
英超直播
英超直播
英超直播
英超直播
英超直播