【发布时间】:2021-03-08 15:30:53
【问题描述】:
我创建了一个代码,用于在音乐列表 (music_list) 中选择随机音乐并将它们添加到 music_queue。当我执行这段代码时,music_list 的所有元素都被删除了,我不明白为什么。
print("Music list lenght : " + str(len(music_list)))
if len(music_queue) == 0:
tmp = music_list
while len(tmp) > 0:
music_queue.append(tmp.pop(randint(0,len(tmp) - 1)))
print("Music list lenght : " + str(len(music_list)))
【问题讨论】:
-
这能回答你的问题吗? python: changes to my copy variable affect the original variable 或者更好的是,该问题的副本:stackoverflow.com/questions/2612802/…
-
改用
tmp = music_list.copy()。对于列表,“=”运算符会复制同一对象,因此在您编辑副本时会编辑原始对象。
标签: python python-3.x