有人知道为什么当我运行这段代码时,会将同一个字典条目追加100次吗
from random import choice
aliens = []
alien = {}
colors = ['red', 'blue', 'green', 'black', 'purple', 'brown', 'yellow', 'coral']
points = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
speeds = ['slow', 'medium', 'fast']
for i in range(1,101):
alien['color'] = choice(colors)
alien['points'] = choice(points)
alien['speed'] = choice(speeds)
aliens.append(alien)
print(aliens)
该链接存储字典和列表,并将其添加到另一个列表中,您只需创建指向同一字典的新链接。只需使用
alien.copy()
相关问题 更多 >
编程相关推荐