Python 测试是否存在任何集合元素在列表中

Python 测试是否存在任何集合元素在列表中

在本文中,我们将学习如何在Python中检查任何集合元素是否存在于列表中。

使用的方法

  • 使用any()函数

  • 使用位运算符&操作符

  • 使用Counter()、filter()和lambda函数

示例

假设我们有一个输入集合和一个输入列表。现在,我们将使用上述方法检查任何输入集合元素是否存在于输入列表中。

输入

inputSet = {4, 8, 1, 3, 5, 7}
inputList = [7, 15, 20]

输出

Checking whether any set element present in the input list: True

在上面的示例中,7存在于集合和列表中,因此结果为True。

方法1:使用any()函数

any()函数返回 True ,如果可迭代对象中的任何项为真,则返回True,否则返回False。

语法

any(iterable)

步骤

以下是执行所需任务的算法/步骤:

  • 创建一个变量来存储 输入集合 ,并打印给定的集合。

  • 创建另一个变量来存储 输入列表

  • 使用 any() 函数通过遍历输入集合并检查当前元素是否在输入列表中来检查输入列表中是否存在任何集合元素。

  • 以布尔值形式打印结果。

示例

以下程序使用 any() 函数检查是否存在任何输入集合元素在输入列表中,并返回True或False:

# input set
inputSet = {4, 8, 1, 3, 5, 7}

# printing the input set
print("Input set:\n", inputSet)

# input list
inputList = [7, 15, 20]

# checking whether any set element is present in the input list using any() function
result = any(i in inputSet for i in inputList)

# printing the output
print("Checking whether any set element present in the input list:", result)

输出

执行后,上述程序将生成以下输出 –

Input set:
{1, 3, 4, 5, 7, 8}
Checking whether any set element present in the input list: True

方法2:使用按位与运算符

按位与运算符 - “&” 是一种比较数字(二进制)的按位运算符。如果两个位都为1,则将每个位设置为 1

步骤

以下是要执行所需任务的算法/步骤:

  • 使用set()函数将给定的输入转换为集合。

  • 使用 & 运算符(如果两个位都为1,则将每个位设置为1)检查输入列表中是否存在任何集合元素,并使用 bool() 函数将结果转换为布尔值(返回给定对象的布尔值)。

  • 打印结果。

示例

以下程序使用按位 & 运算符检查输入集合中是否存在任何输入列表元素,并返回True(存在)或False(不存在)。

# input set
inputSet = {4, 8, 1, 3, 5, 7}

# printing the input set
print("Input set:\n", inputSet)

# input list
inputList = [9, 15, 20]

# Convert the given list to set using the set() function
inputListSet = set(inputList)

# checking whether any set element present in the input list

# using & operator(checks for common element) and converting to boolean
result = bool(inputSet & inputListSet)

# printing the output
print("Checking whether any set element present in the input list:", result)

输出

执行上述程序将生成以下输出:

Input set:
{1, 3, 4, 5, 7, 8}
Checking whether any set element present in the input list: False

方法3:使用Counter(),filter()和lambda函数

filter()函数 - 使用确定序列中的每个元素是真还是假的函数来过滤指定的序列。

Counter()函数 - 一个子类,用于计算可哈希对象。在调用/调用时,它会隐式地创建可迭代对象的哈希表。

lambda()函数

lambda函数是一个小型的匿名函数。

lambda函数可以有无限/任意数量的参数,但只能有一个表达式。

语法

lambda arguments : expression

步骤

以下是执行所需任务的算法/步骤:

  • 使用import关键字从collections模块中导入Counter函数。

  • 使用Counter()函数将所有输入列表元素的频率作为字典获取。

  • 使用filter函数根据上述频率字典过滤所有输入集合元素。

  • 如果存在任何公共元素,则过滤后的列表长度将大于1。

  • 使用if条件语句检查上述条件并相应打印。

示例

以下程序使用Counter()、filter()和lambda函数检查输入列表中是否存在任何输入集合元素,并返回True(存在)或返回False(不存在)。

# importing a Counter function from the collections module
from collections import Counter

# input set
inputSet = {4, 8, 1, 3, 5, 7}

# printing the input set
print("Input set:\n", inputSet)

# input list
inputList = [7, 15, 20, 7]

# getting the frequency of list elements using the Counter() function

# Here it returns frequencies as a dictionary
elements_freq = Counter(inputList)

# Traversing in the input Set using the lambda function

# Checking if the set element exists in the keys of the dictionary

# Filtering all the elements which satisfy the above condition
output = list(filter(lambda k: k in elements_freq.keys(), inputSet))

# Check if there are any filtered elements
if(len(output) > 0):
   output = True

# If no elements are common then the output will be False
else:
   output = False

# printing the output
print("Checking whether any set element present in the input list:", output)

输出

运行以上程序将生成以下输出 –

Input set:
{1, 3, 4, 5, 7, 8}
Checking whether any set element present in the input list: True

结论

在本文中,我们学习了如何使用三种不同的方法来确定集合是否包含列表中的元素。我们还学习了如何使用set()函数将任何可迭代对象(例如列表、元组或其他可迭代对象)转换为集合,并且学习了如何使用&运算符找到两个给定集合中的共同元素。

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程