【发布时间】:2019-01-30 16:17:54
【问题描述】:
当试图在setOfsets 中查找集合的交集时,会出现下面提到的错误。
示例 setA = [['1','2'], ['2,3'], '['2','4']]
我知道它不能有“Lists”数据类型的交集,我尝试将它转换为一个集合,但它不起作用
def multiple_set_intersection(*sets):
"""Return multiple set intersection."""
try:
return set.intersection(*sets)
except TypeError: # this is Python < 2.6 or no arguments
pass
try: a_set= sets[0]
except IndexError: # no arguments
return set() # return empty set
return reduce(a_set.intersection, sets[1:])
multiple_set_intersection(*setOfSets)
预期结果:setOfSets 内部存在的所有集合的交集,即 intersection = 2
实际结果:
return reduce(a_set.intersection, sets[1:])
AttributeError: 'list' object has no attribute 'intersection'
【问题讨论】:
-
请分享列表,这样更容易回答
-
setA并且其中的元素是列表而不是集合 -
那么消耗的输出是多少?
-
请分享
setOfSets的完整列表 -
列表列表是一个数组
标签: python python-3.x set