I have the following sorted lists of tuples:
list1 = [(0.2, 'a'), (0.4, 'b'), (0.5,'d')]
list2 = [(0.1, 'a'), (0.3, 'c'), (0.7, 'x')]
list3 = [(0.5, 'c'), (0.6, 'a'), (0.5, 'b')]
I want to create an overall ranked list based on the common letters as follows:
- If the letter is common in all three lists, add the three individual values
- If the letter is only common between two lists, add the two individual values and a 1
- If the element is only in one list, add 2 to its value
Expected result:
[(0.9, 'a'), (1.8, 'c'), (1.9, 'b'), (2.5, 'd'), (2.7, 'x')]
What is working:
I am able to get the expected result if the item is common in all three lists but I am unable to get correct results if it's the other cases.
Code snippet
list1 = [(0.2, 'a'), (0.4, 'b'), (0.5, 'd')]
list2 = [(0.1, 'a'), (0.3, 'c'), (0.7, 'x')]
list3 = [(0.5, 'c'), (0.6, 'a'), (0.5, 'b')]
priority_result = [] # when element is common in all 3 lists
twos_array = [] #when element is common in only two lists
result = [(s1, l1 + l1) for (l1, s1), (l1, s2) in zip(list1, list2)]
print(result)
for (score, resultID) in list1:
for (score1, resultID1) in list2:
for (score2, resultID2) in list3:
if(resultID == resultID1 or resultID == resultID2):
result = [(score + score1 + score2, resultID)]
priority_result.extend(result)
elif(resultID == resultID1 and resultID != resultID2):
result = [(score + score1 + 1, resultID)]
twos_array.extend(result)
How can I work on this to produce the desired outcome?