error in returning duplicates after loop through results

时间:2016-04-04 16:38:37

标签: python-3.x

This is my code currently

def buildPointsTable(teams, results):   
teams=['Colombia', 'Japan', 'Mexico', 'France']
results=['Mexico, France, 4, 6', 'Japan, Mexico, 8, 2', 'Colombia, Mexico, 3, 9', 'Colombia, Japan, 9, 7', 'Colombia, France, 2, 8', 'Japan, France, 4, 4']
points=[]
score=0


for t in results:
    t=t.split(', ')
    if t[2]>t[3]:
        score=score+3

        if t[0] not in points:
            points.append([t[0], score])

        else:
            points.append(score)



    elif t[2]<t[3]:
        score=score+3

        if t[1] not in points:
            points.append([t[1], score])

        else:
            points.append(score)



    elif t[2]==t[3]:
        score=score+1

        if t[0] not in points:
            points.append([t[0], score])

        elif t[1] not in points:
            points.append([t[1], score])

        else:
            points.append(score)



    score=0
return points

This is what I am returning

[['France', 3], ['Japan', 3], ['Mexico', 3], ['Colombia', 3], ['France', 3], ['Japan', 1]]

I need 'teams' that win (if they score higher 3 is added, if they tie 1 is added) twice or win and tie to not return separately. As above I returned ['France', 3] twice because they won twice. Results is in the format [t1,t2,s1,s2] where s1 is score of t1 and s2 is score of t2

1 个答案:

答案 0 :(得分:1)

Try this:

scores = dict.fromkeys(teams,0)

and when incrementing scores:

scores[t[0]]+=3

or

scores[t[1]]+=3

etc.

This will make a dictionary with the teams as keys and give every team a score of 0. Then it will do a lookup on the winning team name and increment the value of its score.