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

WBOY
发布: 2023-09-04 21:49:04
转载
1550人浏览过

python程序用于测试列表中是否存在任何集合元素

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

使用的方法

示例

假设我们已经采用了输入集输入列表。我们现在将使用上述方法检查输入列表中是否存在任何输入集元素。

输入

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程序用于测试列表中是否存在任何集合元素的详细内容,更多请关注php中文网其它相关文章!

python速学教程(入门到精通)
python速学教程(入门到精通)

python怎么学习?python怎么入门?python在哪学?python怎么学才快?不用担心,这里为大家提供了python速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载
来源:tutorialspoint网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号