python - 如何将集合列表作为单独的参数传递给函数?

标签 python list function set

<分区>

我已经创建了一个集合列表,我想将其传递给 set.intersection()

例如:

List_of_Sets = [{1,2,3},{3,4,5},{5,6,7}]
set.intersection(List_of_Sets)

结果:

TypeError: descriptor 'intersection' requires a 'set' object but received a 'list'

期望的输出:

{3,5}

我如何将列表中的每个集合作为单独的参数传递给 set.intersection()?

最佳答案

使用解包操作符:set.intersection(*List_of_Sets)


正如另一个答案中所指出的,列表中没有交叉点。您要计算相邻元素交集的并集吗?

>>> set.union(*[x & y for x, y in zip(List_of_Sets, List_of_Sets[1:])])
set([3, 5])

关于python - 如何将集合列表作为单独的参数传递给函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21326749/

相关文章:

python - 为什么 MySQL 连接查询的输出作为嵌套列表项返回?

R 函数,访问参数名称

python - SymPy 是否失去了除以多个多项式的功能?

Python 用户输入平均值

python - 在 Python 中对字典(使用日期键)进行排序

python - 如何在 tkinter 中正确保存文件

r - 通过与列表变量值匹配来改变列

c# - 对两个 List<SelectListItem> 进行正确的并集

c - 如何编写一个函数来永久更改传递的字符串

python - (R-->R) 函数的简单自动分类