"Roelof Wobben" <rwob...@hotmail.com> wrote
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 = {}
Here you create your dictionary objects.
You never create any more dictionary objects so these are the only
ones you have.
for wedstrijd in tournooi :
if wedstrijd['thuis'] in stand :
print "True"
stand is a list of dictionaries so this will never be 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)
Here you append the dictionary into stand
if wedstrijd['uit'] in stand :
print "True"
But stand is a list with a dictionary inside so this test
cannot be true since wedstrijg['uit'] is not a dictionary.
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)
Now you append a second dictionary to stand.
On the next iteration you overwrite those two dictionaries
with new values then append them to the list again.
So you wind up with 2 copies of the updated dictionaries.
So the data of A and B are overwriting by C and D.
How can I prevent this ?
You need to create new dictionaries for each iteration of the loop.
Move the dictionary creation lines inside the loop.
HTH,
--
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/
_______________________________________________
Tutor maillist - Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor