Re: [Tutor] FW: list of dict question

2010-10-09 Thread Francesco Loffredo



On 08/10/2010 19.20, Roelof Wobben wrote:

...
Oke,

What I try to achieve is this :

1) Look if a team is known in stand.
2) If no, take care that the team is known in stand (this part I have written 
with your help)
3) if yes, make wedstrijden one more so wedstrijden is equal to number of 
played games of that team
and update the rest of the data

With 3 I was thinking about this steps.

1) First search where the team is in stand.
You are very near to this: when you searched the team in stand, you 
could as well take note of what was your index in stand at the point 
where you found it. If you didn't find it, you have just added that team 
to stand, so its position must be stand[len(stand)-1]
(here a point about multi-user environments and the possibility of stand 
being modified on-the-fly should be added, but let's ignore it for now)

2) Update the data

Once you have a position, AND THE NUMBER OF PLAYED GAMES, this is trivial:

stand[position][wedstrijden] = numPlayedGames

You shoul NOT add one to wedstrijden, because every time that you run 
your program, for whatever reason, you are updating the record. And 
maybe you just wanted to test it! If you count the number of played 
games every time, you are sure that  the correct number is written on 
stand, even if you run the program thousands of times.
I imagine that you can count the played games searching in tournooi, but 
I'm not sure of it. You should keep that count somewhere.




Roelof

Francesco
Nessun virus nel messaggio in uscita.
Controllato da AVG - www.avg.com
Versione: 9.0.862 / Database dei virus: 271.1.1/3184 -  Data di rilascio: 
10/08/10 08:34:00
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] FW: list of dict question

2010-10-08 Thread Francesco Loffredo

On 08/10/2010 16.54, Roelof Wobben wrote:

...
Hello Franceso,

Thank you for the answer.

You're welcome.


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 ?
I'm afraid it cannot, Roelof. In your loop, you are searching many teams 
(if I translated well, I don't even know what language you speak) in the 
wrong place. If you try the following at a Python shell prompt:



stand =
[{'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'}]

stand['ploeg']

Traceback (most recent call last):
  File "", line 1, in 
TypeError: list indices must be integers, not str




you'll see that stand['ploeg'] doesn't exist. What does exist is, for 
example, stand[0]['ploeg'], or stand[3]['ploeg']. This is because stand 
is a list, and it just contains dictionaries. See my previous example 
with apples and baskets. So, if you are searching stand for all elements 
NOT CONTAINING a given team, you should search wedstrijd['thuis'] in the 
'ploeg' element of EACH element of stand, something like:


teller = 1
for wedstrijd in tournooi:
  for roelof in range(len(stand)):
if wedstrijd['thuis'] != stand[roelof]['ploeg']:
  teller = teller + 1


stand[teller]['wedstrijd'] += 1


This cannot work, either, because no element in stand contains an 
element whose key is 'wedstrijd', and I don't understand why you should 
update that element of stand whose index is teller. In this program, 
teller ends up containing the TOTAL number of elements in stand not 
containing the "thuis" of EACH "wedstrijd" of turnooi. I don't 
understand what do you want to do with this statement. It's very 
probable that teller becomes much larger than len(stand), and so 
stand[teller] will raise an exception...




Roelof  


Francesco
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
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] FW: list of dict question

2010-10-08 Thread Roelof Wobben




> From: rwob...@hotmail.com
> To: f...@libero.it
> Subject: RE: [Tutor] list of dict question
> Date: Fri, 8 Oct 2010 14:53:53 +
>
>
>
>
> 
> Date: Fri, 8 Oct 2010 13:40:04 +0200
> From: f...@libero.it
> To: tutor@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@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
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor