Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #99337

Re: Finding scores from a list

From BartC <bc@freeuk.com>
Newsgroups comp.lang.python
Subject Re: Finding scores from a list
Date 2015-11-24 14:12 +0000
Organization A noiseless patient Spider
Message-ID <n31r4d$a6g$1@dont-email.me> (permalink)
References <277843f7-c898-4378-85ea-841b09a289e3@googlegroups.com>

Show all headers | View raw


On 24/11/2015 13:25, Cai Gengyang wrote:
>
> results = [
> {"id": 1, "name": "ensheng", "score": 10},
> {"id": 2, "name": "gengyang", "score": 12},
> {"id": 3, "name": "jordan", "score": 5},
> ]
>
> I want to find gengyang's score. This is what I tried :
>
>>>> print((results["gengyang"])["score"])
>
> but I got an error message instead :
>
> Traceback (most recent call last):
>    File "<pyshell#62>", line 1, in <module>
>      print((results["gengyang"])["score"])
> TypeError: list indices must be integers, not str
>
> Any ideas how to solve this? Thank you ..

Your new results type is a list of dicts.

Lists are indexed by a number. That will be results[1] for the dict 
relevant to "gengyang" (not even the 2 of the "id", because lists start 
counting at 0).

To find the entry for particular name, you have to search for it (for a 
list entry where the dict key "name" gives you the value you expect, ie. 
"gengyang" in this example).

So a dict of dicts was a better idea.

-- 
Bartc

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Finding scores from a list Cai Gengyang <gengyangcai@gmail.com> - 2015-11-24 05:25 -0800
  Re: Finding scores from a list Mark Lawrence <breamoreboy@yahoo.co.uk> - 2015-11-24 13:44 +0000
  Re: Finding scores from a list Peter Otten <__peter__@web.de> - 2015-11-24 14:50 +0100
  Re: Finding scores from a list BartC <bc@freeuk.com> - 2015-11-24 14:12 +0000
  Re: Finding scores from a list John Gordon <gordon@panix.com> - 2015-11-24 17:47 +0000

csiph-web