Python 查找最常见的元素集合
在Python中,集合是一组无序且唯一的元素,使用{}表示。它允许高效的成员测试和消除重复值,因此对于去除重复项或检查集合之间的共同元素非常有用。
在本文中,我们将学习使用Python编写一个查找最常见元素集合的程序。
使用的方法
以下是完成此任务的各种方法:
- 使用for循环和set.intersection()函数
- 使用列表推导式、max()和intersection()函数
示例
假设我们已经通过输入列表包含集合。现在我们将使用上述方法获取最常见的元素。
输入
inputList = [{9, 1, 3, 2}, {4, 5, 8, 1},
{1, 4, 7, 8}, {9, 5, 5, 4}]
argumentSet = {5, 4, 9, 1}
输出
Input list: [{9, 2, 3, 1}, {8, 1, 4, 5}, {8, 1, 4, 7}, {9, 4, 5}]
The most common elements in the input set are: {8, 1, 4, 5}
在给定的参数集合{5,4,9,1}中,集合{8,1,4,5}包含最常见的元素,即(4,5,1)。
步骤
以下是执行所需任务的算法/步骤:
- 创建一个变量来存储 输入列表 的 集合 。
-
打印输入列表。
-
创建另一个变量来存储 输入参数集合 。
-
使用 set() 函数(创建一个空集合对象。集合列表的顺序是随机的,因为项目没有排序。它会删除所有重复项)创建一个空集合,用于存储结果集。
-
将最大长度初始化为0。
-
使用 for循环 遍历输入列表的每个元素。
-
使用 if条件 语句来检查公共元素的数量是否大于最大长度变量。
-
如果为真,则将最大长度赋值为这些公共元素的数量。
-
将当前集合分配为结果。
-
打印包含输入列表中最常见元素的结果集。
示例1:使用for循环和set.intersection()函数
在此示例中,我们将使用for循环和set.intersection()函数来查找给定集合中最常见的元素。
set.intersection()函数: intersection()方法返回一个包含两个或多个集合之间相似性的集合。
这意味着仅包含在两个集合中存在的项目,或者在比较两个以上的集合时,包含在所有集合中的集合。
len()函数: len()方法返回对象中的项目数。当对象为字符串时,len()函数返回字符串中的字符数。
示例
以下程序使用for循环和set.intersection()函数返回一个包含输入集合中最常见元素的集合。
# input list of sets
inputList = [{9, 1, 3, 2}, {4, 5, 8, 1},
{1, 4, 7, 8}, {9, 5, 5, 4}]
# printing the input list
print("Input list:", inputList)
# input argument set
argumentSet = {5, 4, 9, 1}
# creating an empty set for storing resultant set
resultantSet = set()
# initiliazing maximum length as 0
maxLength = 0
# traversing through the input list
for i in inputList:
# Checking the common elements is greater than the maximum length variable
if len(i.intersection(argumentSet)) > maxLength:
# If the above condition is true then add these common elements to the max length
maxLength = len(i.intersection(argumentSet))
# assigning a current element to the resultant set
resultantSet = i
# printing resultant Set
print("The Most common elements in the input set are:", resultantSet)
输出
执行上述程序后,将生成以下输出结果
Input list: [{9, 2, 3, 1}, {8, 1, 4, 5}, {8, 1, 4, 7}, {9, 4, 5}]
The most common elements in the input set are: {8, 1, 4, 5}
示例2: 使用列表推导、max()和intersection()函数
在这个方法中,我们将学习如何使用列表推导、max()和intersection()函数的简单组合来查找最常见的元素集合。
列表推导
当你希望基于现有列表的值构建一个新的列表时,列表推导提供了一种更短、更简洁的语法。
max()函数
max() 方法返回可迭代对象中的最大值/最大数字。以下程序使用列表推导、max()和intersection()函数返回一个包含输入集合中最常见的元素的集合。
# input list of sets
inputList = [{9, 1, 3, 2}, {4, 5, 8, 1},
{1, 4, 7, 8}, {9, 5, 5, 4}]
# printing the input list
print("Input list:", inputList)
# input argument set
argumentSet = {5, 4, 9, 1}
# Get the most number of common elements from the given list of sets
maxLength = max(len(i.intersection(argumentSet)) for i in inputList)
# Getting the set with the above number of common elements
resultantSet = [i for i in inputList if len(i.intersection(argumentSet)) == maxLength][0]
# printing resultant Set
print("Most common elements in input set are:", resultantSet)
输出
执行上述程序后,将生成以下输出:
Input list: [{9, 2, 3, 1}, {8, 1, 4, 5}, {8, 1, 4, 7}, {9, 4, 5}]
The most common elements in the input set are: {8, 1, 4, 5}
结论
在这篇文章中,我们学习了两种不同的方法来从给定的一组集合中找出最常见的元素集合。此外,我们还学习了如何使用交集函数从给定的两个集合中提取共同的元素。