Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/heroku/2.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 - Fatal编程技术网

如何将列表中的一项与此列表中的所有其他项进行比较,python

如何将列表中的一项与此列表中的所有其他项进行比较,python,python,Python,我有这样一份清单: all = [[a,b,c,d],[r,d,g,s],[e,r,a,b],[p,o,i,u]....(more similar items)] for i in range(len(all)): print len(all[i] & all[i+1]) ##how many identical items shared by all[0] and all[1] print len(all[i+1] & all[i+2]) 我想知道其

我有这样一份清单:

 all = [[a,b,c,d],[r,d,g,s],[e,r,a,b],[p,o,i,u]....(more similar items)]
 for i in range(len(all)):
     print len(all[i] & all[i+1]) ##how many identical items shared by all[0] and all[1]
     print len(all[i+1] & all[i+2])
我想知道其中有多少项是相同的,所以我需要将
all[0]
all[1]、all[2]…all[(len(all)-1)]
进行比较,然后使用
all[1]
all[2]、all[3]…all[(len(all)-1)]
进行比较,然后
all[2]、
all[2]、[4]、…all[(len 1

我试过这样的方法:

 all = [[a,b,c,d],[r,d,g,s],[e,r,a,b],[p,o,i,u]....(more similar items)]
 for i in range(len(all)):
     print len(all[i] & all[i+1]) ##how many identical items shared by all[0] and all[1]
     print len(all[i+1] & all[i+2])
但我不知道如何继续,我想得到的结果是:

item1 has 3 same values with item2, 
      has 4 same values with item3,
      has 1 same values with item4....

item2 has 3 same values with item1,
      has 2 same values with item3,
      etc

这里最简单的算法是n^2。只需在列表上循环两次:

for x, left in enumerate(all):
    for y, right in enumerate(all):
        common = len(set(left) & set(right))
        print "item%s has %s values in common with item%s"%(x, common, y)

基本上,您要做的是计算每个列表中元素集与其他列表的交点长度。试试这个:

a = [['a','b','c','d'],['r','d','g','s'],['e','r','a','b'],['p','o','i','u']]

for i in range(len(a)):
   for j in range(len(a)):
      print "item%d has %d same values as item%d" % ( i, len(set(a[i]) & set(a[j])) ,j )

输出格式并不完全是您想要的,但您已经有了想法。

设置就是一种方法。
 
all = [[1,2,3,4],[1,2,5,6],[4,5,7,8],[1,8,3,4]]
set_all = [set(i) for i in all]
for i in range(len(all)):
    for j in range(len(all)):
        if i == j: 
            continue
        ncom = len(set_all[i].intersection(set_all[j]))
        print "List set %s has %s elements in common with set %s" % (i, ncom, j)

List set 0 has 2 elements in common with set 1 List set 0 has 1 elements in common with set 2 List set 0 has 3 elements in common with set 3 List set 1 has 2 elements in common with set 0 List set 1 has 1 elements in common with set 2 List set 1 has 1 elements in common with set 3 List set 2 has 1 elements in common with set 0 List set 2 has 1 elements in common with set 1 List set 2 has 2 elements in common with set 3 List set 3 has 3 elements in common with set 0 List set 3 has 1 elements in common with set 1 List set 3 has 2 elements in common with set 2


all=[[1,2,3,4]、[1,2,5,6]、[4,5,7,8]、[1,8,3,4]]
set_all=[设置(i)为i的集合]
对于范围内的i(len(all)):
对于范围内的j(len(all)):
如果i==j:
持续
ncom=len(集合所有[i]。交点(集合所有[j]))
打印“列表集%s与集%s共有%s个元素”%(i,ncom,j)

列表集0有2个元素与集1相同 列表集0有1个与集2相同的元素 列表集0有3个与集3相同的元素 列表集1有2个元素与集0相同 列表集1有1个元素与集2相同 列表集1有1个元素与集合3相同 列表集2有1个与集0相同的元素 列表集2有1个元素与集合1相同 列表集2有2个元素与集合3相同 列表集3有3个元素与集0相同 列表集3有1个元素与集合1相同 列表集3有2个元素与集合2相同


如果你在寻找最短的答案,因为你和我一样懒惰:)


是否有子列表包含相同的值两次?如果是这样,您关心多重性吗?@DSM子列表不包含重复项:)顺便说一句,您确实不应该使用名称
all
,因为您正在覆盖