如何通过字典交集高效找出三个已排序数组中共有的元素?

2026-04-02 12:151阅读0评论SEO教程
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计466个文字,预计阅读时间需要2分钟。

如何通过字典交集高效找出三个已排序数组中共有的元素?

Python | 使用字典交集找到三个排序数组中的公共元素原文:Python | 通过字典交集中找到三个排序数组中的公共元素原文链接:https://www.geeksforgeeks.org/python-dictionary-intersection-find-common-elements-three-sorted-arrays/

Python|通过字典交集找到三个排序数组中的公共元素原文:h Python |通过字典交集找到三个排序数组中的公共元素

原文: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 中快速解决这个问题。方法很简单,

  • 首先使用 Counter() 方法,将所有三个列表转换成以元素为键,以它们的频率为值的字典。
  • 现在对三个字典执行交集操作,这将导致我们的字典在三个数组列表中具有公共元素及其频率。
  • # 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]

    本文共计466个文字,预计阅读时间需要2分钟。

    如何通过字典交集高效找出三个已排序数组中共有的元素?

    Python | 使用字典交集找到三个排序数组中的公共元素原文:Python | 通过字典交集中找到三个排序数组中的公共元素原文链接:https://www.geeksforgeeks.org/python-dictionary-intersection-find-common-elements-three-sorted-arrays/

    Python|通过字典交集找到三个排序数组中的公共元素原文:h Python |通过字典交集找到三个排序数组中的公共元素

    原文: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 中快速解决这个问题。方法很简单,

  • 首先使用 Counter() 方法,将所有三个列表转换成以元素为键,以它们的频率为值的字典。
  • 现在对三个字典执行交集操作,这将导致我们的字典在三个数组列表中具有公共元素及其频率。
  • # 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]