Python – 打印所有子列表中的共同元素

Python – 打印所有子列表中的共同元素

Python语言主要由不同的数据结构组成,其中列表数据结构是最常用的。列表可以容纳不同数据类型的元素,并且一旦赋值就无法更改。列表甚至可以在方括号“[]”中容纳其他列表。当两个列表被定义为在某个平台上使用时,它们之间可能存在某种关系,使用这段代码可以实现Python程序。

打印子列表中的共同元素

假设有一个包含10个元素的列表,也可以以子列表的形式表示,例如一个子列表包含3个元素,另一个子列表包含剩下的7个元素。

语法

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

基本语法包含列表数据结构和子列表。

方法

方法1—使用递归函数

方法2—使用reduce函数

方法3—使用迭代方法

方法1:使用递归函数打印所有子列表中的公共元素的Python程序

使用递归函数来打印列表的子列表中的公共元素,该函数通过递归调用自身。

算法

  • 步骤1 −创建common_elements()函数。

  • 步骤2 −当列表的长度等于1时,它设置列表的第一个元素。

  • 步骤3 −否则,它使用切片方法设置交集函数来匹配子列表以找到公共元素。

  • 步骤3 −它返回带有公共元素的语句。

示例

#function is defined with a list argument
def common_elements(list1):
    if len(list1) == 1:
        return set(list1[0])
    else:
        return set(list1[0]).intersection(common_elements(list1[1:]))
#initializing the sublist inside the list data structure
list1 = [[2,3,4,5], [2,5], [2,10,5], [2,8,5,6], [2,10,9,5]]
common_elements = common_elements(list1)
#the length of the list should be greater than 0
if len(common_elements)>0:
    print("The common elements in the given sublists is:",common_elements)
else:
    print("The given list does not contain common elements! ")

输出结果

The common elements in the given sublist is: {2, 5}

方法二:使用reduce函数打印所有子列表中的共同元素的Python程序

在打印共同元素的方法中,不需要任何复杂的步骤,导入functools模块以使用reduce函数。

算法

  • 步骤1 - 导入所需的模块。

  • 步骤2 - 使用子列表初始化列表数据结构以找到它们之间的共同元素。

  • 步骤3 - 通过使用reduce和intersection函数实现此问题的主要功能。

  • 步骤4 - map函数将元素映射并将每个子列表转换为一个集合。

  • 步骤5 - 根据定义的输入,打印语句将返回子列表中的共同元素。

示例

#importing the module
from functools import reduce
#initializing the sublist inside the list data structure
list1 = [[2,3,4,5], [2,5], [2,10,5], [2,8,5,6], [2,10,9,5]]
#with the help of the intersection function to match the elements among sublists
common_elements = reduce(set.intersection,map(set,list1))
#the length of the list should be greater than 0
if len(common_elements)>0:
    print("The common elements in the given sublists is:",common_elements)
else:
    print("The given list does not contain common elements! ")

输出

The common elements in the given sublist is: {2, 5}

方法3:使用for循环迭代方法打印共同元素的Python程序

通过对列表进行迭代,输出打印出子列表之间共同元素的所有可能性。

算法

  • 步骤1 - 创建一个具有一个列表参数的函数。

  • 步骤2 - 然后创建一个空集,并将数组的第一个元素作为list1[0]。

  • 步骤3 - 使用for循环进行迭代。

  • 步骤4 - 验证条件是否长度大于零。

  • 步骤5 - 调用common_element函数,输出语句被打印出。

示例

#defining the function with one list argument
def common_element(list1):
#creating an empty set and taking the first element of the array
    reference_set=set(list1[0])
#for is used to iterate through the given list
    for num in range(1,len(list1)):
#Adding the common element from the list to the created set
        fresh_set=set(tuple(list1[num]))
        reference_set&=fresh_set
#To check whether the length of the list greater than 0
    if len(reference_set) > 0:
        print("The common elements in the given sublists is:",reference_set)
    else:
        print("The given list does not contain common elements! ")
#initializing the list of sublists 
list1 = [[11,3,12,5], [12,11], [12,11,5], [12,11,5,6], [12,11,9,5]]
#calling the function to print the common elements
common_element(list1)

输出

The common elements in the given sublist is: {11, 12}

结论

要找到子列表中的共同元素,需要了解子列表是如何分配和其基本结构的。列表包含由方括号内的公共元素分隔的元素。通过使用模块、递归函数和简单的迭代方法,可以解释打印列表的子列表中的共同元素的方法。

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程