且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Python:在多个列表中查找相同的项目

更新时间:2023-02-17 20:21:07

与手工操作相同的方式:

The same way as you'd do it by hand:

seen = set()
repeated = set()
for l in list_of_lists:
  for i in set(l):
    if i in seen:
      repeated.add(i)
    else:
      seen.add(i)

顺便说一句,这是一些人正在寻找的一种班轮(不计入进口额)(应该比另一种方式效率低)

By the way, here's the one liner (without counting the import) that some people were seeking (should be less efficient than the other approach)

from itertools import *
reduce(set.union, (starmap(set.intersection, combinations(map(set, ll), 2))))