例:
postings = [[1, 100, 142, 322, 12312],
[2, 100, 101, 322, 1221],
[100, 142, 322, 956, 1222]]
想得到这样的输出:
[100, 322]
发布于 2018-01-31 16:28:05
import heapq, itertools
def intersect(*its):
for key, values in itertools.groupby(heapq.merge(*its)):
if len(list(values)) == len(its):
yield key
>>> list(intersect(*postings))
[100, 322]
发布于 2018-01-31 17:21:19
def postings(posts):
sets = (set(l) for l in posts)
return sorted(reduce(set.intersection, sets))
在python中实现的逻辑可能会比上面的要好一些。
https://stackoverflow.com/questions/-100007280
复制相似问题