簡體   English   中英

"Python:在多個列表中查找相同的項目"

[英]Python: Find identical items in multiple lists

我有一個任意數量的列表的列表,例如:

[[1,2,3], [3,4,5], [5,6,7], [7,8,9]]

與您手動執行相同的方式:

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)

順便說一句,這是一些人正在尋找的一個班輪(不包括進口)(應該比另一種方法效率低)

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

最干凈的方法可能是使用reduce:

def findCommon(L):
    def R(a, b, seen=set()):
        a.update(b & seen)
        seen.update(b)
        return a
    return reduce(R, map(set, L), set())

result = findCommon([[1,2,3], [3,4,5], [5,6,7], [7,8,9]])

結果是一個集合,但如果您確實需要一個列表,只需執行list(result)

這只找到所有列表共有的元素(即交集):

 set.intersection(*[set(list) for list in list_of_lists])

參考: http : //docs.python.org/library/stdtypes.html#set

#!/usr/bin/python

ll = [[1,2,3], [3,4,5], [5,6,7], [7,8,9]]
ls = [set(l) for l in ll]

su = ls[0]  #union
ssd = ls[0] #symmetric_difference
for s in ls[1:]:
  su = su.union(s)
  ssd = ssd.symmetric_difference(s)

result = su.difference(ssd)
print list(result)

=>

[3, 5, 7]

修改和采用FP,

ll = [[1,2,3], [3,4,5], [5,6,7], [7,8,9]]

u = reduce(set.union, map(set, ll))
sd = reduce(set.symmetric_difference, map(set, ll))
print u - sd

=>

[3, 5, 7]

嘗試這個:

data = [[1,2,3], [3,4,5], [5,6,7], [7,8,9], [1,2,3]]

res = set()

for i in data:
    for j in data:
        if i is not j:
            res |= set(i) & set(j)

print res

您可以使用字典來獲取每個

from collections import defaultdict

init_list = [[1,2,3], [3,4,5], [5,6,7], [7,8,9]]
#defaultdict, every new key will have a int(0) as default value
d = defaultdict(int)
for values in init_list:
  #Transform each list in a set to avoid false positives like [[1,1],[2,2]]
  for v in set(values):
    d[v] += 1

#Get only the ones that are more than once
final_list = [ value for value,number in d.items() if number > 1 ]
>>> sets = [[1,2,3], [3,4,5], [5,6,7], [7,8,9]]
>>> seen = set()
>>> duplicates = set()
>>> 
>>> for subset in map(set, sets) :
...     duplicates |= (subset & seen)
...     seen |= subset
... 
>>> print(duplicates)
set([3, 5, 7])
>>> 

我嘗試使用 map/reduce 進行單行答案,但還不能完全理解。

我還在學習,但想分享我的答案。

num_list1 = ['3', '6', '5', '8', '33', '12', '7', '4', '72', '2', '42', '13']
num_list2 = ['3', '6', '13', '5', '7', '89', '12', '3', '33', '34', '1', '344', '42']

result = [int(num) for num in num_list1 if num in num_list2)
print(result)
l=[[1,2,3], [3,4,5], [5,6,7], [7,8,9]]
d={}
for x in l:
    for y in x:
        if not d.has_key(y):
            d[y]=0
        d[y]+=1
[x for x,y in d.iteritems() if y>1]

這是我的去處:

seen = set()
result = set()
for s in map(set, [[1,2,3], [3,4,5], [5,6,7], [7,8,9]]):
    result.update(s & seen)
    seen.update(s)
print result

這打印:

set([3, 5, 7])

您可以使用集合,請參閱http://docs.python.org/library/stdtypes.html#set

展平,排序,1 for 循環比較之前和之后的數字

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM