Here is a function that works fine for all cases and return the list of all first candidates encountered if no choice can be made to separate them.
def find_best(list_of_lists):
i = 0
while len(list_of_lists[i]) == 0:
i+=1
list_containing_candidates = list_of_lists[i][:]
if len(list_containing_candidates) == 1 :
return list_containing_candidates[0]
else:
if i+1 < len(list_of_lists):
for next_list in list_of_lists[i+1:]:
for candidate in list_containing_candidates[:]:
if candidate not in next_list:
list_containing_candidates.remove(candidate)
if len(list_containing_candidates) == 0:
list_containing_candidates = list_of_lists[i][:]
elif len(list_containing_candidates) == 1:
return list_containing_candidates[0]
return list_of_lists[i] # ambigous case, entire list of candidates returned
print(find_best([[], [2, 3], [1], [1], [3]])) # 3
print(find_best([[5], [0, 8], [0, 8], [0, 8], [1]])) # 5
print(find_best([[0, 1], [0, 3], [0], [0], [2]])) # 0
print(find_best([[], [3, 6], [3, 5, 6], [6], [1]])) # 6
print(find_best([[], [3, 6], [3, 5], [6], [1]])) # 3
print(find_best([[1,3 ], [1, 3], [1,2,3], [1,3], []])) # [1,3]
5
solved Python deduce best number among list of lists [closed]