-
绝地无双
您的代码将所有重复值附加到新列表,即“a”。要仅保留 1 个重复值,您需要检查列表中是否已经存在相同的值。如果这不存在,那么只有您应该将该值附加到您的列表中。我已经修改了您的代码,以适应这种需要。enter code heresome_list=['a','b','c','b','d','m','n','n']a=[]for items in some_list: if some_list.count(items) > 1 and a.count(items) == 0: a.append(items)print (a)输出:['b', 'n']
-
Cats萌萌
some_list=['a','b','c','b','d','m','n','n']a=[]for items in some_list: if some_list.count(items) > 1: if items not in a: a.append(items)print (a)输出:['b', 'n']或者:some_list=['a','b','c','b','d','m','n','n']a=[]for items in some_list: if some_list.count(items) > 1: a.append(items)a=set(a)print (a)出去:['b', 'n']
-
湖上湖
如果您在if语句中再添加一个条件,您的代码将起作用。some_list = ['a', 'b', 'c', 'b', 'd', 'm', 'n', 'n']a = []for items in some_list: if some_list.count(items) > 1 and items not in a: a.append(items)print (a)输出:-['b', 'n']