I am trying to find the list in nested list, which has the maximum number of highest numbers, as shown bellow:
test_list = [[200,1,100],[50,120,0],[40,300,0]]
desired output = [200,1,100]
[200,1,100]
is the winner here, as out of its 3 digits, 2 are higher than the other lists 2 numbers. This would not be sequence sensitive however, for example if:
test_list = [[1,100,200],[1,200,100],[200,1,100],[50,120,0],[40,300,0]]
desired output = [[1,100,200],[1,200,100],[200,1,100]]
Reason being that here all three constitue the "maximum" lineup of digits. Is there anyway other than creating all permutations and comparing them?
Edit: For further clarification, for the example above, the numbers in the nested list are 300>200>120>100> 50>40>1. So, which list in the nested list has the maxmimum number of highest numbers? [200,1,100]. Does there exist another list in the nested list, with a higher number of highest numbers? No. If [300,200,1] was in the nested list, it would have won.
To provide context, these are scores for a fuzzy match between a series of strings to find closest matches, each providing 3 scores in a list, in term listed in the list of all possible matches. This is my way of closest match between strings, comparing the fuzzy scores and taking the list with the highest possible scores out of the list.
Find maxes by defining:
Code
from functools import cmp_to_key
def compare(sublist1, sublist2):
'''
Comparator function
With >, <, = defined to satisfy the
requirements for comparing sublists
the function:
Returns:
Positive when sublist1 > sublist2
Negative when sublist1 < sublist2
0 when sublsit1 == sublist2
'''
return sum(1 if x > y else -1 for x, y in zip(sorted(sublist1), sorted(sublist2)) if x != y)
def find_max(lst):
# find max value in list (using custom compare function)
# use cmp_to_key to key to convert compare function to key
m = max(lst, key=cmp_to_key(compare))
# Use list comprehension to get all values equal
# max (equal when compare(m, x) == 0)
return [x for x in lst if compare(m, x)==0]
Test
print(find_max([[200,1,100],[50,120,0],[40,300,0]]))
# Output: [[200, 1, 100]]
print(find_max([[1,100,200],[1,200,100],[200,1,100],[50,120,0],[40,300,0]]))
# Output [[1, 100, 200], [1, 200, 100], [200, 1, 100]]
print(find_max([[9, 9, 0], [1, 1, 1]]))
# Output: [[9, 9, 0]]
print(find_max([[100, 100, 44, 100, 100, 56, 38], [100, 100, 100, 100, 100, 100, 73], [100, 100, 100, 100, 100, 100, 73], [100, 100, 100, 100, 100, 56, 41], [100, 100, 100, 100, 100, 56, 41], [100, 100, 100, 100, 100, 56, 41]] ))
# Output: [[100, 100, 100, 100, 100, 100, 73], [100, 100, 100, 100, 100, 100, 73]]