原文:https://www . geesforgeks . org/python-dictionary-交集-find-common-elements-三排序-数组/
给定三个按非递减顺序排序的数组,打印这些数组中的所有公共元素。
示例:
Input: ar1 = [1, 5, 10, 20, 40, 80] ar2 = [6, 7, 20, 80, 100] ar3 = [3, 4, 15, 20, 30, 70, 80, 120]Output: [80, 20]Input: ar1 = [1, 5, 5] ar2 = [3, 4, 5, 5, 10] ar3 = [5, 5, 10, 20]Output: [5, 5]
这个问题我们已经有了解决方案,请参考找到三个排序数组中的公共元素链接。我们可以使用字典的交集在 python 中快速解决这个问题。方法很简单,
# Function to find common elements in three# sorted arraysfrom collections import Counterdef commonElement(ar1,ar2,ar3): # first convert lists into dictionary ar1 = Counter(ar1) ar2 = Counter(ar2) ar3 = Counter(ar3) # perform intersection operation resultDict = dict(ar1.items() & ar2.items() & ar3.items()) common = [] # iterate through resultant dictionary # and collect common elements for (key,val) in resultDict.items(): for i in range(0,val): common.append(key) print(common)# Driver programif __name__ == "__main__": ar1 = [1, 5, 10, 20, 40, 80] ar2 = [6, 7, 20, 80, 100] ar3 = [3, 4, 15, 20, 30, 70, 80, 120] commonElement(ar1,ar2,ar3)
输出:
[80, 20]