[Tutor] list of dict question
Roelof Wobben
rwobben at hotmail.com
Fri Oct 8 09:39:54 CEST 2010
Hello,
I have this programm :
tournooi = [{'thuis': 'A','uit': "B",'thuisscore': 20, 'uitscore': 15},{'thuis': 'C','uit': "D",'thuisscore': 80, 'uitscore': 40}]
stand = []
tussen_thuis = {}
tussen_uit = {}
for wedstrijd in tournooi :
if wedstrijd['thuis'] in stand :
print "True"
else :
tussen_thuis['ploeg'] = wedstrijd['thuis']
tussen_thuis['wedstrijden'] = 1
if wedstrijd['thuisscore']> wedstrijd['uitscore']:
tussen_thuis['punten'] = 2
else:
tussen_thuis['punten'] = 0
tussen_thuis['voor'] = wedstrijd['thuisscore']
tussen_thuis ['tegen'] = wedstrijd['uitscore']
stand.append(tussen_thuis)
if wedstrijd['uit'] in stand :
print "True"
else :
tussen_uit['ploeg'] = wedstrijd['uit']
tussen_uit['wedstrijden'] = 1
if wedstrijd['thuisscore'] < wedstrijd['uitscore']:
tussen_uit['punten'] = 2
else:
tussen_uit['punten'] = 0
tussen_uit['tegen'] = wedstrijd['thuisscore']
tussen_uit ['voor'] = wedstrijd['uitscore']
stand.append(tussen_uit)
It gives this output :
[{'punten': 2, 'tegen': 40, 'wedstrijden': 1, 'voor': 80, 'ploeg': 'C'}, {'punten': 0, 'tegen': 80, 'wedstrijden': 1, 'voor': 40, 'ploeg': 'D'}, {'punten': 2, 'tegen': 40, 'wedstrijden': 1, 'voor': 80, 'ploeg': 'C'}, {'punten': 0, 'tegen': 80, 'wedstrijden': 1, 'voor': 40, 'ploeg': 'D'}]
So the data of A and B are overwriting by C and D.
How can I prevent this ?
Roelof
More information about the Tutor
mailing list