[Tutor] FW: list of dict question

Roelof Wobben rwobben at hotmail.com
Fri Oct 8 16:54:57 CEST 2010




----------------------------------------
> From: rwobben at hotmail.com
> To: fal at libero.it
> Subject: RE: [Tutor] list of dict question
> Date: Fri, 8 Oct 2010 14:53:53 +0000
>
>
>
>
> ----------------------------------------
> Date: Fri, 8 Oct 2010 13:40:04 +0200
> From: fal at libero.it
> To: tutor at python.org
> Subject: Re: [Tutor] list of dict question
>
>
> Il 08/10/2010 10.02, Alan Gauld ha scritto:
>>
>> "Roelof Wobben" 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)
>
> _______________________________________________
> Tutor maillist - Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>

Hello Franceso,

Thank you for the answer.

Now find ot how i can find the dict which contains a team.
I thinking now of something like this.

teller = 1
  For wedstrijd in tournooi :
    if wedstrijd['thuis'] != stand ['ploeg'] :
    teller = teller + 1
stand[teller]['wedstrijd'] += 1

Could this work ?

Roelof 		 	   		  


More information about the Tutor mailing list