I have just started learning python.
I'm trying to compare elements in the list. For example I have a list:
list = [['red', 'blue', 'black'], ['red', 'blue', ' white'], ['red', 'pink']]
Now, how can I compare element 0: ['red','blue','black']
with the rest of elements in the list and print those elements with the biggest count of matches, like the most matching element is ['red', 'blue', ' white']
next ['red', 'pink']
Update:
At this point I managed to do something like this:
mylist = [set(item) for item in list]
for i, item in enumerate(mylist):
for i1 in xrange(i + 1, len(mylist)):
for val in (item & mylist[i1]):
print "Index {} matched with index {} for value
{}".format(i,i1,val)
if i == 0:
print list[(i1)]
Output:
Index 0 matched with index 1 for value "Red"
['red', 'blue', ' white']
Index 0 matched with index 1 for value "Blue"
['red', 'blue', ' white']
...
I have found a solution: Python: Compare elements in a list to each other.
Any help would be much appreciated. Thanks.