Returning a result from 3 items in a list
Cai Gengyang
gengyangcai at gmail.com
Tue Nov 24 05:27:43 EST 2015
Strange, it gives me an error message when i type result["jordan"] or result[max(result)] though :
>>> results = {
"gengyang": 14,
"ensheng": 13,
"jordan": 12
}
>>> result["jordan"]
Traceback (most recent call last):
File "<pyshell#27>", line 1, in <module>
result["jordan"]
NameError: name 'result' is not defined
>>> result[max(result)]
Traceback (most recent call last):
File "<pyshell#28>", line 1, in <module>
result[max(result)]
NameError: name 'result' is not defined
>>>
On Tuesday, November 24, 2015 at 6:14:43 PM UTC+8, Chris Angelico wrote:
> On Tue, Nov 24, 2015 at 9:04 PM, Cai Gengyang <gengyangcai at gmail.com> wrote:
> > Here's a dictionary with 3 values :
> >
> > results = {
> > "gengyang": 14,
> > "ensheng": 13,
> > "jordan": 12
> > }
> >
> > How do I define a function that takes the last of the 3 items in that list and returns Jordan's results i.e. (12) ?
> >
> > Thanks a lot !
>
> If you want Jordan's result, that's easy:
>
> result["jordan"]
>
> But there's no concept of "the last" entry in a dict. They don't have
> an order. Do you mean "the entry with the greatest key"? That could be
> written thus:
>
> result[max(result)]
>
> because max(result) is the string "jordan".
>
> Does either of those make sense for what you're doing?
>
> ChrisA
More information about the Python-list
mailing list