3

I have two lists (can be more later), and i want to figure out, which values mach at the same position.

This code below returns matched values, but its not returning the position of the match.

a = [5,0]
b = [5,1]

print list(set(a).intersection(set(b)))

>>5
user1767754
  • 23,311
  • 18
  • 141
  • 164

6 Answers6

4

Use zip and enumerate and check for unique values:

lists = [a, b] # add more lists here if need be...
for idx, items in enumerate(zip(*lists)):
    unique = set(items)
    if len(unique) == 1:
        # idx = position, unique.pop() == the value
        print idx, unique.pop()
Jon Clements
  • 138,671
  • 33
  • 247
  • 280
  • What is the * for here? – user1767754 Jun 04 '15 at 22:19
  • 1
    It's called unpacking - it allows the zip function to be called with a variable number of arguments as specified in `lists` - see http://stackoverflow.com/questions/36901/what-does-double-star-and-star-do-for-python-parameters for full info. – Jon Clements Jun 04 '15 at 22:22
  • 1
    The * allows a variable number of arguments. Allowing one to pass as many lists as needed into the function. The beauty of this solution is that it easily handles the case where lists are not all the same length. – steve Jun 04 '15 at 22:34
  • 1
    @steve yup - it will automatically stop on the shortest list... of course... `izip_longest` is also available if that shouldn't be the case... – Jon Clements Jun 04 '15 at 22:36
2
def check_equal(lst):
   return lst[1:] == lst[:-1]

def get_position_and_matches(*lists):
  shortest_list = min(lists, key=len)
  for index,item in enumerate(shortest_list):
    matching = [l[index] for l in lists]
    if check_equal(matching):
      print "Index: {0}, Value: {1}".format(index, shortest_list[index])

one = [1, 3, 4, 6, 2]
two = [1, 3, 4, 2, 9, 9]
three = [2, 3, 4]
get_position_and_matches(one, two, three)
steve
  • 2,488
  • 5
  • 26
  • 39
1

You could write your own method:

a = [1, 2, 3, 4, 5]
b = [5, 4, 3, 2, 1]
c = [3, 3, 3, 3, 3]
allLists = [b, c] # all lists but the first

for i in range(len(a)):
    good = True
    for l in allLists:
        if l[i] != a[i]:
            good = False
            break
    if good:
        print(i, a[i])

edited to make it easier to add more lists

user1767754
  • 23,311
  • 18
  • 141
  • 164
Ryan
  • 2,058
  • 1
  • 15
  • 29
1

This will show you the position of the match (assuming the value None is not a valid element)

a=[1,2,3]
b=[0,2,3]
c=[3,2,1]
l = [a, b, c] # add any number of lists
z = zip(*l)
pos = 0
for i in z:
    if reduce(lambda x, y: x if x == y else None, i):
        print pos
    pos += 1

or, if you wanted to keep the match for each position:

matches=[reduce(lambda x, y: x if x == y else None, i) for i in z]

would produce

[None, 2, None]
Pynchia
  • 10,996
  • 5
  • 34
  • 43
0
matching_indexes = [i for i in range(len(a)) if a[i] == b[i] == c[i]]

Can be used. A simple list comprehension to test each individual value of a,b and c. More or less == can be added for each list to be compared. However, this assumes all the lists are of the same length or that a is the shortest list.

TheStrangeQuark
  • 2,257
  • 5
  • 31
  • 58
0

This is the answer to as many lists as you want to use

a = [5,0,1,2]
b = [5,2,3,2]
lists = [a,b,b,a,a]
d = dict()
for l in lists:
    for i in range(len(a)):
        if i not in d.keys():
            d[i] = a[i]
        elif d[i] != l[i]:
            d[i] = -1
for i in d.keys():
    if d[i] != -1:
        print d[i], i
Endzior
  • 164
  • 9