[Tutor] FW: list of dict question
Francesco Loffredo
fal at libero.it
Fri Oct 8 19:00:35 CEST 2010
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 "<stdin>", line 1, in <module>
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
-------------- 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