I'm currently looking for a way to compare elements of a list to one another going from left to right.
Here is my list:
mylist = [[15], [14, 15], [19, 20], [13], [3], [65, 19], [19, 20, 31]]
I need to compare the first element to all others and check if any of the values match, same thing for the second and third element and so on until it reaches the end of the list. If there is a match, I need to print it out. I need to print out the value that has match and the index that it matched at.
For example.
index 0 matched with index 1 for value 15.
index 2 matched with index 5 for value 19
index 2 matched with index 6 for value 19
index 2 matched with index 6 for value 20
index 5 matched with index 6 for value 19.
How can I go about doing this?
The naive solution is to loop over every pair, which is slow. However, you can do something along the lines of this:
This is what I mean:
>>> mylist = [[15], [14, 15], [19, 20], [13], [3], [65, 19], [19, 20, 31]]
>>>
>>> pairs = dict()
>>>
>>>
>>> from collections import defaultdict
>>>
>>> pairs = defaultdict(list)
>>>
>>> for idx, sub in enumerate(mylist):
... for a in sub:
... pairs[a].append(idx)
...
>>> pairs
defaultdict(<type 'list'>, {65: [5], 3: [4], 13: [3], 14: [1], 15: [0, 1], 19: [2, 5, 6], 20: [2, 6], 31: [6]})
You can disregard the value lists with only 1 element (since that means we don't have a pair). Those with 2+ elements form a variety of pairs. For instance with 19
we have [2, 5, 6]
meaning:
as you have. This approach is linear in the total number of items within your nested lists. If you have a reasonable upper bound on the nested list lengths, then this is O(n) in the size of your master list.
Now for the output:
>>> from itertools import combinations
>>>
>>> for k,v in pairs.iteritems():
... if len(v) > 1:
... for a,b in combinations(v, 2):
... print "Index {} matched with index {} for value {}".format(a, b, k)
...
Index 0 matched with index 1 for value 15
Index 2 matched with index 5 for value 19
Index 2 matched with index 6 for value 19
Index 5 matched with index 6 for value 19
Index 2 matched with index 6 for value 20
You can use itertools.combinations
, it saves you from the nested-loop:
In [770]: l2=[(i,set(mylist[i])) for i in range(len(mylist))]
...: for (i, a), (j, b) in combinations(l2,2):
...: for v in a.intersection(b):
...: print "Index {} matched with index {} for value {}".format(i,j,v)
Index 0 matched with index 1 for value 15
Index 2 matched with index 5 for value 19
Index 2 matched with index 6 for value 19
Index 2 matched with index 6 for value 20
Index 5 matched with index 6 for value 19
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With