卓越飞翔博客卓越飞翔博客

卓越飞翔 - 您值得收藏的技术分享站
技术文章64334本站已运行4115

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

方法一:使用any()函数

如果可迭代对象中的任何一项为 true,则 any() 函数返回 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 关键字从集合模块导入 Counter 函数。

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

  • 使用过滤器函数过滤所有输入集元素(如果它们存在于上述频率字典中)。

  • 如果存在任何共同元素,则过滤后的列表的长度将大于 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() 函数将任何可迭代对象(例如列表、元组或任何可迭代对象)转换为集合,以及如何使用 & 运算符查找这两个集合共有的元素给予。

卓越飞翔博客
上一篇: 如何使用Python在Excel中创建文件、文件夹和子文件夹的列表?
下一篇: 在C++中,通过执行给定的操作将一个数字减少到1

相关推荐

留言与评论(共有 0 条评论)
   
验证码:
隐藏边栏