list = ['cat', 'dog', 'wolf', 'cat', 'wolf', 'cat']
items_map = {}
for item in list:
if item not in items_map.keys():
items_map[item] = 1
else:
items_map[item] += 1
result = []
for key in items_map.keys():
if items_map[key] > 1:
result.append(key)
print(result)
>>> from collections import Counter
>>> words = ['cat', 'dog', 'wolf', 'cat', 'wolf', 'cat', 'sheep', 'sheep']
>>> c = Counter(words)
>>> [k for k, v in c.items() if v > 1]
['cat', 'wolf', 'sheep']
您可以使用^{} 来计算重复次数,然后使用列表理解来仅保留计数大于1的重复次数
相关问题 更多 >
编程相关推荐