[Tutor] list of dict question

Francesco Loffredo fal at libero.it
Fri Oct 8 13:40:04 CEST 2010


Il 08/10/2010 10.02, Alan Gauld ha scritto:
>
> "Roelof Wobben" <rwobben at hotmail.com> wrote
>
>> I have this programm :
>>
>> tournooi = [{'thuis': 'A','uit': "B",'thuisscore': 20, 'uitscore':
> ...
>> for wedstrijd in tournooi :
>> if wedstrijd['thuis'] in stand :
>> print "True"
>
> stand is a list of dictionaries so this will never be True.
>...
>> 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.
>
I'll say the same another way: you are searching an apple in a container 
of baskets, one of which MAY CONTAIN an apple. But you only see baskets, 
and none of those can BE an apple!
My two cents: the following might be still new for you, but this is a 
way to check if wedstrijd['thuis'] is in stand:

if wedstrijd['thuis'] in [u['ploeg'] for u in stand]

where you build a temporary list of 'ploeg's in stand and check whether 
wedstrijd['thuis'] is found there.

>...
> 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.
> ...
This is difficult for me too: why does this happen? Or, more correctly, 
why should this happen? How can you save the current contents of a 
dictionary in a list, making sure that the saved values won't change if 
you update the dict?
You tell Roelof that the dictionary must be created at every loop, but 
if so, where goes the elegance of
     myDictList.append(UpdateIt(myDict))
???

Francesco (puzzled)
-------------- next part --------------

Nessun virus nel messaggio in uscita.
Controllato da AVG - www.avg.com 
Versione: 9.0.862 / Database dei virus: 271.1.1/3182 -  Data di rilascio: 10/07/10 08:34:00


More information about the Tutor mailing list