2024-04-26 14:49:33 发布
网友
我尝试了以下代码,但似乎没有删除任何重复的代码
list2 = [element for element in list1 if element not in list2]
但是,
for i in list1: if i not in list2: list2.append(i)
这个代码运行得很好,有人能告诉我为什么会这样吗
它生成的列表与list2在运行时不包含任何元素的列表相同。你想要的是:
list1 = [1, 2, 3, 3, 5, 9, 6, 2, 8, 5, 2, 3, 5, 7, 3, 5, 8] list2 = [] [list2.append(item) for item in list1 if item not in list2] print(list2)
它生成的列表与list2在运行时不包含任何元素的列表相同。你想要的是:
相关问题 更多 >
编程相关推荐