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


Groups > comp.lang.python > #99332

Re: Finding scores from a list

Path csiph.com!news.mixmin.net!newsreader4.netcologne.de!news.netcologne.de!fu-berlin.de!uni-berlin.de!not-for-mail
From Peter Otten <__peter__@web.de>
Newsgroups comp.lang.python
Subject Re: Finding scores from a list
Date Tue, 24 Nov 2015 14:50:01 +0100
Organization None
Lines 37
Message-ID <mailman.115.1448373011.2291.python-list@python.org> (permalink)
References <277843f7-c898-4378-85ea-841b09a289e3@googlegroups.com>
Mime-Version 1.0
Content-Type text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding 7Bit
X-Trace news.uni-berlin.de v/mpQMKokRM/8JJrbJUzvwJLz18lkQVElDlPucDut5pQ==
Return-Path <python-python-list@m.gmane.org>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.004
X-Spam-Evidence '*H*': 0.99; '*S*': 0.00; 'indices': 0.07; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'typeerror:': 0.09; 'python': 0.10; 'integers,': 0.16; 'item:': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:io': 0.16; 'received:plane.gmane.org': 0.16; 'received:psf.io': 0.16; 'received:t-ipconnect.de': 0.16; 'wrote:': 0.16; 'first,': 0.20; '"",': 0.22; 'matching': 0.23; 'tried': 0.24; '(most': 0.24; 'header:User-Agent:1': 0.26; 'subject:list': 0.26; 'header:X-Complaints-To:1': 0.26; 'error': 0.27; 'container': 0.29; 'str': 0.29; 'traceback': 0.33; 'file': 0.34; 'this?': 0.34; 'list': 0.34; 'text': 0.35; 'skip:p 30': 0.35; 'but': 0.36; 'instead': 0.36; 'to:addr:python-list': 0.36; 'subject:: ': 0.37; 'received:org': 0.37; 'skip:p 20': 0.38; 'thank': 0.38; 'subject:from': 0.39; 'to:addr:python.org': 0.40; 'received:de': 0.40; 'some': 0.40; 'provide': 0.61; 'note:': 0.66; 'results': 0.66; 'picture.': 0.84; 'score.': 0.84
X-Injected-Via-Gmane http://gmane.org/
X-Gmane-NNTP-Posting-Host p57bd9007.dip0.t-ipconnect.de
User-Agent KNode/4.13.3
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.20+
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Xref csiph.com comp.lang.python:99332

Show key headers only | View raw


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.

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