Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to compare one item in a list with all the other items in this list, python

Tags:

python

I have a list like this:

 all = [[a,b,c,d],[r,d,g,s],[e,r,a,b],[p,o,i,u]....(more similar items)]

I want to get how many items are the same among them, so I need to compare all[0] with all[1],all[2]...all[(len(all)-1)], and then use all[1] to compare with all[2],all[3]...all[(len(all)-1)], then all[2] to compare with all[3],all[4],...all[(len(all)-1)]

I tried something like this:

 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])

but don't know how to continue, The result I want to get is:

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
like image 910
manxing Avatar asked Aug 10 '12 13:08

manxing


1 Answers

The simplest algorithm here is a n^2. Just loop over your list twice:

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)
like image 58
stderr Avatar answered Oct 01 '22 17:10

stderr