Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 找出是否有两个集合具有相同的成员资格_Python_Python 3.x_Set - Fatal编程技术网

Python 找出是否有两个集合具有相同的成员资格

Python 找出是否有两个集合具有相同的成员资格,python,python-3.x,set,Python,Python 3.x,Set,我有六套,名为s1。。。s6,每个都有几百个字符串元素 我想测试它们中是否有任何一个具有与其他任何元素相同的元素集。我该怎么设置呢 值得一提的是,我从这里开始,试图打印一个罪犯的索引: setlist = [s1, s2, s3, s4, s5, s6] for i in range(len(setlist)): if any([setlist[i] == st for st in setlist[i+1:]]): print('You have redundant s

我有六套,名为s1。。。s6,每个都有几百个字符串元素

我想测试它们中是否有任何一个具有与其他任何元素相同的元素集。我该怎么设置呢

值得一提的是,我从这里开始,试图打印一个罪犯的索引:

setlist = [s1, s2, s3, s4, s5, s6]

for i in range(len(setlist)):
    if any([setlist[i] == st for st in setlist[i+1:]]):
        print('You have redundant sets: %i' % i)

为了澄清,s1-s6均为set()类型。我需要能够看到哪些匹配,例如,如果s2和s3恰好有相同的成员,我希望看到
“您有多余的集合:s2,s3”
显然,我的起始代码无法让我达到目的。

一种解决方案可能是使用集合内置数据结构并比较每个集合。为此,你可以把你所有的集合放进一本字典,并给出(1,2,3,5,6)键。 要获得用于比较的所有排列,您可以使用itertools.product,例如:

import itertools
sets_1 = range(1,7)
sets_2 = range(1,7)
perms = list(filter(lambda x: x!=0, [(a,b) if a!= b else 0 for (a, b) in list(itertools.product(*[sets_1, sets_2]))]))
通过使用perms,您可以执行以下操作:

sets_dict= {1:{2,3}, 2:{5,6}, 3:{7,8}, 4:{5,9}, 5:{10,22}, 6:{22,4}}
for (set_1, set_2) in perms:
    print(sets_dict[set_1] & sets_dict[set_2]) 

或者,如果您的集合太大,您可以循环遍历每个集合,并填写字典,其中键是集合成员,值是承载它们的集合。

一个解决方案可能是使用集合内置数据结构并比较每个集合。为此,你可以把你所有的集合放进一本字典,并给出(1,2,3,5,6)键。 要获得用于比较的所有排列,您可以使用itertools.product,例如:

import itertools
sets_1 = range(1,7)
sets_2 = range(1,7)
perms = list(filter(lambda x: x!=0, [(a,b) if a!= b else 0 for (a, b) in list(itertools.product(*[sets_1, sets_2]))]))
通过使用perms,您可以执行以下操作:

sets_dict= {1:{2,3}, 2:{5,6}, 3:{7,8}, 4:{5,9}, 5:{10,22}, 6:{22,4}}
for (set_1, set_2) in perms:
    print(sets_dict[set_1] & sets_dict[set_2]) 

或者,如果您的集合太大,您可以循环遍历每个集合,并填写一个字典,其中键是集合成员,值是承载它们的集合。

如果您想找到所有具有共同元素的集合对,这应该会有所帮助。您将希望使用内置的set函数,而不是进行元素比较

have_overlap = list()
setlist = [s1, s2, s3, s4, s5]
for ix1,s in enumerate(setlist):
  for ix2 in range(ix+1, len(setlist)):
    if s.symmetric_difference(setlist [ix2]):
      have_overlap.append(ix, ix2)

如果您想找到所有具有共同元素的集合对,这应该会有所帮助。您将希望使用内置的set函数,而不是进行元素比较

have_overlap = list()
setlist = [s1, s2, s3, s4, s5]
for ix1,s in enumerate(setlist):
  for ix2 in range(ix+1, len(setlist)):
    if s.symmetric_difference(setlist [ix2]):
      have_overlap.append(ix, ix2)

有很多方法可以做到这一点。您可以迭代集合的数量,对于每2个集合,检查它们是否不相交

s1.isdisjoint(s2)
如果两个集合中没有公共元素,则上述函数返回True

s1 & s2
上面的表达式返回集合s1和s2中的公共元素

any(i in s1 for i in s2)

如果一个元素在两个集合中是公共的,则上述生成器表达式返回布尔值True。对于上述所有表达式,生成器表达式是最快的。第二快的是
isdisjoint()
函数。设置交叉口
s1&s2
花费的时间最长,因此也是最慢的。

有多种方法可以做到这一点。您可以迭代集合的数量,对于每2个集合,检查它们是否不相交

s1.isdisjoint(s2)
    s1={1,2,3}
    s2={2,3,4}
    s3={1,2,3}
    s4={1,2,3}
    s5={2,6,7}
    setlist = [s1, s2, s3, s4,s5]
    redundantSet=set()
    for i in range(len(setlist)):
        for j in range(i+1,len(setlist)):
            if(setlist[i]==setlist[j]):
                if(i==0):
                    redundantSet.add(i)
                redundantSet.add(j)
    print('You have redundant sets at index' + str(redundantSet))
如果两个集合中没有公共元素,则上述函数返回True

s1 & s2
上面的表达式返回集合s1和s2中的公共元素

any(i in s1 for i in s2)
如果一个元素在两个集合中是公共的,则上述生成器表达式返回布尔值True。对于上述所有表达式,生成器表达式是最快的。第二快的是
isdisjoint()
函数。设置交叉口s1和s2的时间最长,因此也是最慢的

    s1={1,2,3}
    s2={2,3,4}
    s3={1,2,3}
    s4={1,2,3}
    s5={2,6,7}
    setlist = [s1, s2, s3, s4,s5]
    redundantSet=set()
    for i in range(len(setlist)):
        for j in range(i+1,len(setlist)):
            if(setlist[i]==setlist[j]):
                if(i==0):
                    redundantSet.add(i)
                redundantSet.add(j)
    print('You have redundant sets at index' + str(redundantSet))
不能使用任何()函数来等值,因为任何()只接受列表,即使列表中有一个元素,也会返回true。即使这样做

any[(setlist[i+1:],setlist[i])
将返回true,因为“setlist[i]”元素将在那里。因此,将返回true

不能使用任何()函数来等值,因为任何()只接受列表,即使列表中有一个元素,也会返回true。即使这样做

any[(setlist[i+1:],setlist[i])

将返回true,因为“setlist[i]”元素将在那里。因此,将返回true。

您是否需要知道违规者或是否有违规者?请添加示例输入+输出。也许您需要使用内置类型。您是否需要知道违规者或是否有违规者?请添加示例输入+输出。也许您需要使用内置类型。