0

If I had the two following lists with tuples in the form of (x,y):

 [(a,1),(b,2),(c,7),(d,1)]
 [(a,3),(b,2),(c,7),(d,8)]

I want to count the number of differences with respect to 'y' value for corresponding 'x' values. In the above case, the answer is 2

(a,1) doesn't match with (a,3)

(d,1) doesn't match with (d,8)

EDIT: This is not a duplicate, the position of the elements matter. I want to check if element 1 in list 1 is same as element 1 in list 2 and so on.

Rakesh Adhikesavan
  • 11,966
  • 18
  • 51
  • 76
  • 1
    this might have been answered here: http://stackoverflow.com/questions/16138015/python-comparing-two-lists – keda Oct 15 '15 at 20:36
  • 1
    Possible duplicate of [python find difference between two lists](http://stackoverflow.com/questions/22559627/python-find-difference-between-two-lists) – Leb Oct 15 '15 at 20:38
  • THis is not a duplicate. I am trying to do something different here – Rakesh Adhikesavan Oct 15 '15 at 20:39

2 Answers2

2

You can use zip function and a generator expression within sum function :

count=sum(i!=j for i,j in zip(list1,list2))
Mazdak
  • 105,000
  • 18
  • 159
  • 188
0

Another method is

x = [("a", 1) ,("b", 2), ("c", 7), ("d", 1)]
y = [("a", 3), ("b", 2), ("c", 7), ("d", 8)]
count = len(set(x).intersection(y))
Shawn Mehan
  • 4,513
  • 9
  • 31
  • 51
  • This doesn't take into account position does it? (that x[1] != y[1]), just that there is a difference somewhere in x respective to y? – stian Nov 09 '15 at 08:31