我有一个清单,上面有几个刺,有些是重复的。我需要提取所有重复的字符串,并将它们附加到新列表中。我该怎么办?
list_i = ['a','b','a','c','a','c','g','w','s','c','d','a','b','c','a','e']
答案 0 :(得分:0)
使用OrderedDict
获取没有重复项的列表,然后将其从原始副本中删除
from collections import OrderedDict
list_i = ['a','b','a','c','a','c','g','w','s','c','d','a','b','c','a','e']
non_dupes = list(OrderedDict.fromkeys(list_i))
dupes = list(list_i)
for d in non_dupes:
dupes.remove(d)
print(dupes)
#['a', 'a', 'c', 'c', 'a', 'b', 'c', 'a']
print(non_dupes)
#['a', 'b', 'c', 'g', 'w', 's', 'd', 'e']