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() 函数?
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])
map
:set.union(*map(operator.__and__,*zip(List_of_Sets, List_of_Sets[1:])))
- mgilson>>> List_of_Sets = [{1,2,3},{3,4,5},{5,6,7}]
>>> set.intersection(*List_of_Sets) # * unpacks list into arguments
set([])
在该集合中不存在交集,因此返回一个空集合。以下是一个可行的示例:
>>> List_of_Sets2 = [{1,2,3},{3,4,5},{5,6,3}]
>>> set.intersection(*List_of_Sets2) # * unpacks list into arguments
set([3])
(注:此处为原文链接,无需翻译)
{1,2,3} intersect {3,4,5} intersect {5,6,7}
的结果不是{3,5}
而是{}
... - shx2