如何在任何(可能不是全部)列表中获取重复值
How to get duplicate values across any, potentially not all, lists
我有一个数据集:
data.append(['a', 'b', 'c'], ['a', 'x', 'y', z'], ['a', 'x', 'e', 'f'], ['a'])
我搜索了 SO 并找到了使用 intersection_update()
在所有列表中 return 重复的方法(所以,在这个例子中,'a'
),但我实际上想 return 来自 any 列表的重复项,即:
retVal = ['a', 'x']
因为 'a'
和 'x'
在所有列表中至少重复一次。是否有内置的 Python 2.7 可以做到这一点?
使用 Counter
to determine the number of each item and chain.from_iterable
将项目从子列表传递到 Counter
。
from itertools import chain
from collections import Counter
data=[['a', 'b', 'c'], ['a', 'x', 'y', 'z'], ['a', 'x', 'e', 'f'], ['a']]
c = Counter(chain.from_iterable(data))
retVal = [k for k, count in c.items() if count >= 2]
print(retVal)
#['x', 'a']
我有一个数据集:
data.append(['a', 'b', 'c'], ['a', 'x', 'y', z'], ['a', 'x', 'e', 'f'], ['a'])
我搜索了 SO 并找到了使用 intersection_update()
在所有列表中 return 重复的方法(所以,在这个例子中,'a'
),但我实际上想 return 来自 any 列表的重复项,即:
retVal = ['a', 'x']
因为 'a'
和 'x'
在所有列表中至少重复一次。是否有内置的 Python 2.7 可以做到这一点?
使用 Counter
to determine the number of each item and chain.from_iterable
将项目从子列表传递到 Counter
。
from itertools import chain
from collections import Counter
data=[['a', 'b', 'c'], ['a', 'x', 'y', 'z'], ['a', 'x', 'e', 'f'], ['a']]
c = Counter(chain.from_iterable(data))
retVal = [k for k, count in c.items() if count >= 2]
print(retVal)
#['x', 'a']