Mik*_*ike 5 python list duplicates sublist
我有一个包含数字的列表(子列表)列表,我只想保留所有(子)列表中存在的列表.
例:
x = [ [1, 2, 3, 4], [3, 4, 6, 7], [2, 3, 4, 6, 7]]
output => [3, 4]
Run Code Online (Sandbox Code Playgroud)
我怎样才能做到这一点?
common = set(x[0])
for l in x[1:]:
common &= set(l)
print list(common)
Run Code Online (Sandbox Code Playgroud)
要么:
import operator
print reduce(operator.iand, map(set, x))
Run Code Online (Sandbox Code Playgroud)
在一个班轮:
>>> reduce(set.intersection, x[1:], set(x[0]))
set([3, 4])
Run Code Online (Sandbox Code Playgroud)