samedi 27 mars 2021

Python comparison of tuples inside a list

I have this exercise that requires us to compare two lists of tuples.

The first list (games) has 3 tuples, each one corresponding to a game match, with the names of 2 people.

The second list (results) has also 3 tuples, each one corresponding to the results of each match (ordered with games).

games = [("anna", "ross"), ("manny", "maria"), ("rita", "joel")]
results = [(2, 0), (1, 3), (1, 1)]

The desired output is supposed to be this:

['anna', 'maria', 'TIE']

When both scores for a match are the same, the output 'TIE'.

I tried to solve it with the zip function, but for some reason I can't iterate properly in all the levels of the tuples.

1st try:

lst = []
i = 0
for a, b in zip(games, results):
    if b[i] == max(b):
        lst.append(a[i])
    elif min(b) == max(b):
        lst.append("TIE")
i = i + 1
print(lst)

This returned ['anna', 'rita'], which made me think there was some issue with iterating the list / tuples using b[i].

Then I tried to do this:

lst = []
for i, c in list(zip(games, results)):
    for j, k in i, c:
        if k == max(c):
            lst.append(i[j])
        elif min(c) == max(c):
            lst.append('TIE')
print(lst)

This returned ['maria', 'TIE', 'joel'], so I suppose the issue is not only with the iteration but maybe with the way that I'm making the comparison (using max function to find the highest score).

Can anyone give any tips or hints to help me move forward to the solution? I'm asking only after spending a long time looking for a similar problem online and not being able to find anything that could really help.

Aucun commentaire:

Enregistrer un commentaire