Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #99375
| Path | csiph.com!xmission!news.snarked.org!news.linkpendium.com!news.linkpendium.com!panix!gordon |
|---|---|
| From | John Gordon <gordon@panix.com> |
| Newsgroups | comp.lang.python |
| Subject | Re: Finding scores from a list |
| Date | Tue, 24 Nov 2015 17:47:17 +0000 (UTC) |
| Organization | PANIX Public Access Internet and UNIX, NYC |
| Lines | 36 |
| Message-ID | <n327r4$3v1$1@reader1.panix.com> (permalink) |
| References | <277843f7-c898-4378-85ea-841b09a289e3@googlegroups.com> |
| NNTP-Posting-Host | panix2.panix.com |
| X-Trace | reader1.panix.com 1448387237 4065 166.84.1.2 (24 Nov 2015 17:47:17 GMT) |
| X-Complaints-To | abuse@panix.com |
| NNTP-Posting-Date | Tue, 24 Nov 2015 17:47:17 +0000 (UTC) |
| User-Agent | nn/6.7.3 |
| Xref | csiph.com comp.lang.python:99375 |
Show key headers only | View raw
In <277843f7-c898-4378-85ea-841b09a289e3@googlegroups.com> Cai Gengyang <gengyangcai@gmail.com> writes:
> results = [
> {"id": 1, "name": "ensheng", "score": 10},
> {"id": 2, "name": "gengyang", "score": 12},
> {"id": 3, "name": "jordan", "score": 5},
> ]
Okay, this is a list.
> 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
Lists are indexed by number, not by name.
You want something like this:
for result in results:
if result["name"] == "gengyang":
print result["score"]
break
--
John Gordon A is for Amy, who fell down the stairs
gordon@panix.com B is for Basil, assaulted by bears
-- Edward Gorey, "The Gashlycrumb Tinies"
Back to comp.lang.python | Previous | Next — Previous in thread | Find similar | Unroll 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