📜  Python|打印所有子列表中的公共元素

📅  最后修改于: 2022-05-13 01:55:06.201000             🧑  作者: Mango

Python|打印所有子列表中的公共元素

给定一个列表列表,任务是找到所有子列表中共有的元素。

有多种方法可以完成此任务。让我们一一讨论这些方法。

方法#1:使用集合

# Python code to find duplicate element in all 
# sublist from list of list
  
# List of list initialization
Input = [ [10, 20, 30, 40],
          [30, 40, 60, 70],
          [20, 30, 40, 60, 70],
          [30, 40, 80, 90], ]
  
Output = set(Input[0])
for l in Input[1:]:
    Output &= set(l)
  
# Converting to list
Output = list(Output)
  
# Printing answer
print(Output)
输出:
[40, 30]


方法#2:使用reducemap

# Python code to find duplicate element in all 
# sublist from list of list
import operator
from functools import reduce
  
# List of list initialization
Input = [ [10, 20, 30, 40],
          [30, 40, 60, 70],
          [20, 30, 40, 60, 70], 
          [30, 40, 80, 90], ]
  
# using reduce and map
out = reduce(operator.iand, map(set, Input))
  
# Converting into list
out = list(out)
  
# Printing output
print(out)
输出:
[40, 30]


方法#3:使用set.intersection

# Python code to find duplicate element in all 
# sublist from list of list
  
# importing reduce 
from functools import reduce
  
# function for set intersection
def func(a, b):
    return list(set(a).intersection(set(b)))
  
# List of list initialization
Input = [ [10, 20, 30, 40],
          [30, 40, 60, 70],
          [20, 30, 40, 60, 70], 
          [30, 40, 80, 90], ]
  
# using reduce and set.intersection
out = reduce(func, Input)
  
# Printing output
print(out)
输出:
[40, 30]