
clist1 = list("oeasy")
clist2 = clist1
clist2 = clist1.copy()
lst1 + lst2"动词打次" + "东东打次"
"动词打次" * 3 + "东东打次"
("动词打次" * 3 + "东东打次") * 4
import random
# 从四大名著中分别提取角色和事迹
# 《西游记》
journey_to_the_west_characters = ["孙悟空", "唐僧", "沙僧"]
journey_to_the_west_stories = ["大闹天宫", "三打白骨精", "真假美猴王"]
# 《红楼梦》
dream_of_the_red_chamber_characters = ["林黛玉", "贾宝玉", "薛宝钗"]
dream_of_the_red_chamber_stories = ["黛玉葬花", "宝玉挨打", "宝钗扑蝶"]
# 《三国演义》
romance_of_the_three_kingdoms_characters = ["刘备", "关羽", "诸葛亮"]
romance_of_the_three_kingdoms_stories = ["桃园结义", "草船借箭", "空城计"]
# 《水浒传》
water_margin_characters = ["宋江", "武松", "鲁智深"]
water_margin_stories = ["怒杀阎婆惜", "景阳冈打虎", "倒拔垂杨柳"]
# 汇总角色列表
all_characters = journey_to_the_west_characters + \
dream_of_the_red_chamber_characters + \
romance_of_the_three_kingdoms_characters + \
water_margin_characters
# 汇总事迹列表
all_stories = journey_to_the_west_stories + \
dream_of_the_red_chamber_stories + \
romance_of_the_three_kingdoms_stories + \
water_margin_stories
# 随机搭配角色和事迹
random_character = random.choice(all_characters)
random_story = random.choice(all_stories)
print(f"{random_character}——{random_story}")
# 汇总角色列表
all_characters = journey_to_the_west_characters + \
dream_of_the_red_chamber_characters + \
romance_of_the_three_kingdoms_characters + \
water_margin_characterslst1 = list(range(3))
print("lst1:", lst1)
lst2 = [3, 4, 5]
print("lst2:", lst2)
print("lst1 + lst2:", lst1 + lst2)
lst1 = list("oeasy")
lst2 = list("o2z")
print(lst1 + lst2) 没了
没
- 咋办呢?lst1 = list("oeasy")
lst2 = list("o2z")
lst3 = lst1 + lst2 垃圾回收 了
垃圾回收呢?
lst3 = lst1 + lst2 lst1 = list("oeasy")
lst2 = list("o2z")
lst1 = lst1 + lst2 
增强赋值
是一种 赋值
在赋值之外 还有增强
lst1 = list("oeasy")
lst2 = list("o2z")
lst1 += lst2 
lst1 = list("oeasy")
print("lst1:", id(lst1))
lst2 = list("o2z")
print("lst2:", id(lst2))
lst1 += lst2
print("lst1:", id(lst1))
lst1 = list("oeasy")
print("lst1:", id(lst1))
lst2 = list("o2z")
print("lst2:", id(lst2))
lst1 = lst1 + lst2
print("lst1:", id(lst1))
效果 相同 - 但效率不同!

lst1 = list("oeasy")
lst2 = list("o2z")
lst1.extend(lst2)
help(list.extend)
区别呢?num_list = [1, 2, 3]
print(num_list)
num_list.append([4, 5])
print(num_list)
num_list.remove([4, 5])
print(num_list)
num_list.extend([4, 5])
print(num_list)

对比项 | append | extend |
|---|---|---|
描述 | 添加的是列表项 | 扩展原始列表 |
特点 | 将元素作为整体添加到列表末尾 | 把新列表对接到原列表尾巴上,合并两个列表 |
地址
- 会改变l1地址
- 后三种不改变地址
2. 效率不同
- 后三种效率高
- 直接扩展列表
加法
- 那有 列表乘法 吗?🤔原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。