Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #99326 > unrolled thread
| Started by | Cai Gengyang <gengyangcai@gmail.com> |
|---|---|
| First post | 2015-11-24 05:25 -0800 |
| Last post | 2015-11-24 17:47 +0000 |
| Articles | 5 — 5 participants |
Back to article view | Back to comp.lang.python
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
| From | Cai Gengyang <gengyangcai@gmail.com> |
|---|---|
| Date | 2015-11-24 05:25 -0800 |
| Subject | Finding scores from a list |
| Message-ID | <277843f7-c898-4378-85ea-841b09a289e3@googlegroups.com> |
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 ..
[toc] | [next] | [standalone]
| From | Mark Lawrence <breamoreboy@yahoo.co.uk> |
|---|---|
| Date | 2015-11-24 13:44 +0000 |
| Message-ID | <mailman.113.1448372708.2291.python-list@python.org> |
| In reply to | #99326 |
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 ..
>
Please read and digest the TypeError. There is a very strong hint in
there, follow it. Compare the type of your data structure `results`
with the one you've used earlier today. Spot the difference?
--
My fellow Pythonistas, ask not what our language can do for you, ask
what you can do for our language.
Mark Lawrence
[toc] | [prev] | [next] | [standalone]
| From | Peter Otten <__peter__@web.de> |
|---|---|
| Date | 2015-11-24 14:50 +0100 |
| Message-ID | <mailman.115.1448373011.2291.python-list@python.org> |
| In reply to | #99326 |
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 ..
As the outer container is a list you have to provide an index:
results[1]["score"]
You can also search for a matching item:
for result in results:
if result["name"] == "gengyang":
print(result["score"])
but when the list grows performance will suffer.
General note: you will have a better experience learning Python when you
read some introductory text first, to get the big picture.
[toc] | [prev] | [next] | [standalone]
| From | BartC <bc@freeuk.com> |
|---|---|
| Date | 2015-11-24 14:12 +0000 |
| Message-ID | <n31r4d$a6g$1@dont-email.me> |
| In reply to | #99326 |
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
[toc] | [prev] | [next] | [standalone]
| From | John Gordon <gordon@panix.com> |
|---|---|
| Date | 2015-11-24 17:47 +0000 |
| Message-ID | <n327r4$3v1$1@reader1.panix.com> |
| In reply to | #99326 |
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"
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web