Python List Comparison

I want to compare two lists and want to know if an element matches another element.

Example: 'a' should match 'b' here it will return True.

list1 = [a,b,c,d] list2 = [b,a,d,c] 

'a' and 'b' correspond to each other (they have the same place in the lists). How do I get a function to return True if they match?

 list1 = [a,b,c,d] list2 = [c,d,a,b] 

This will return False.

+7
source share
8 answers

I would do it like this:

 >>> from operator import eq >>> list1 = ['a','b','c','d'] >>> list2 = ['c','d','a','b'] >>> any(map(eq, list1, list2)) False 

Of course, if you want a complete list of logical โ€œcorrespondence,โ€ you can simply omit the any function:

 >>> map(eq, list1, list2) [False, False, False, False] 
+10
source

First, create an iterator that gives us the position of the a element in list1 :

 # This is a generator expression: it works like a list, but uses O(1) memory a_indexes = (i for i, x in enumerate(list1) if x == a) 

Now, make sure those indexes in list2 compared with b :

 # This returns a boolean b_matches = all(list2[i] == b for i in a_indexes) 

Edit: note that a_indexes , which is a generator, not a list, can only be used once. If you want to reuse the results for something, change () to [] or call list() in the generator.

+2
source

Is this what you want? I assume that a and b appear only once in list1 and list2 respectively.

 def corresponding(list1,list2,a,b): return list1.index(a) == list2.index(b) 
+1
source

Here is an updated version of what I came up with (which suits your use cases):

 def correspond(a, b): """Looks at two lists, if they are the same length and the length is even then it looks to make sure that the pairs are swapped (even if they are moved) >>> print correspond([1,2,3,4], [2,1,4,3]) True >>> print correspond([1,2,3,4], [2,1,4,5]) #One value is out of place False >>> print correspond([1,2,3,4], [2,1,3]) #One value list is shorter False >>> print correspond([1,2,3,4], [3,4,1,2]) #values are moved but not swapped False >>> print correspond("ABCD", "BADC") True """ if len(a) == len(b) and len(a) % 2 == 0: try: for i in xrange(0,len(a),2): if (1+b.index(a[i])) == b.index(a[i+1]): return False return True except ValueError: return False else: return False if __name__ == "__main__": import doctest doctest.testmod() 
+1
source

You can compare the indices of values โ€‹โ€‹that you think should match:

 def in_correspondence(my_list1, my_list2, val1, val2): return my_list1.index(a) == my_list2.index(b) 
0
source

Something like this should give you what you want:

 import itertools as it def are_related(first, second, a, b): a_indexes = (i for i,x in enumerate(first) if x == a) b_indexes = (i for i,x in enumerate(second) if x == b) return all(a_i == b_i for a_i, b_i in it.izip_longest(a_indexes, b_indexes)) 

Note that using itertools.izip_longest takes into account the likelihood of having more than a than b or more than b than a s.

0
source

Your question is incomprehensible, but I think you want:

 first = 'a' second = 'b' list_one = ['a','b','c','d'] list_two = ['b','c','d','e'] if list_one.index(first) == list_two.index(second): print("They correspond") else: print("They don't") 

The above will ignore duplicates ( index returns the position of the first element found).

0
source
 list2[::2] == list1[1::2] and list1[::2] == list2[1::2] 
0
source

All Articles