Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #99305 > unrolled thread
| Started by | Cai Gengyang <gengyangcai@gmail.com> |
|---|---|
| First post | 2015-11-24 02:04 -0800 |
| Last post | 2015-11-25 11:22 +0000 |
| Articles | 13 — 6 participants |
Back to article view | Back to comp.lang.python
Returning a result from 3 items in a list Cai Gengyang <gengyangcai@gmail.com> - 2015-11-24 02:04 -0800
Re: Returning a result from 3 items in a list Chris Angelico <rosuav@gmail.com> - 2015-11-24 21:14 +1100
Re: Returning a result from 3 items in a list Cai Gengyang <gengyangcai@gmail.com> - 2015-11-24 02:27 -0800
Re: Returning a result from 3 items in a list Peter Otten <__peter__@web.de> - 2015-11-24 11:35 +0100
Re: Returning a result from 3 items in a list Cai Gengyang <gengyangcai@gmail.com> - 2015-11-24 02:47 -0800
Re: Returning a result from 3 items in a list Chris Angelico <rosuav@gmail.com> - 2015-11-24 22:05 +1100
Re: Returning a result from 3 items in a list Cai Gengyang <gengyangcai@gmail.com> - 2015-11-24 03:06 -0800
Re: Returning a result from 3 items in a list Peter Otten <__peter__@web.de> - 2015-11-24 11:32 +0100
Re: Returning a result from 3 items in a list Denis McMahon <denismfmcmahon@gmail.com> - 2015-11-24 14:07 +0000
Re: Returning a result from 3 items in a list Mark Lawrence <breamoreboy@yahoo.co.uk> - 2015-11-24 14:28 +0000
Re: Returning a result from 3 items in a list Ned Batchelder <ned@nedbatchelder.com> - 2015-11-24 08:59 -0800
Re: Returning a result from 3 items in a list Cai Gengyang <gengyangcai@gmail.com> - 2015-11-25 02:26 -0800
Re: Returning a result from 3 items in a list Mark Lawrence <breamoreboy@yahoo.co.uk> - 2015-11-25 11:22 +0000
| From | Cai Gengyang <gengyangcai@gmail.com> |
|---|---|
| Date | 2015-11-24 02:04 -0800 |
| Subject | Returning a result from 3 items in a list |
| Message-ID | <133829df-f57f-442f-9dbf-b22b22f656ae@googlegroups.com> |
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 !
Gengyang
[toc] | [next] | [standalone]
| From | Chris Angelico <rosuav@gmail.com> |
|---|---|
| Date | 2015-11-24 21:14 +1100 |
| Message-ID | <mailman.98.1448360068.2291.python-list@python.org> |
| In reply to | #99305 |
On Tue, Nov 24, 2015 at 9:04 PM, Cai Gengyang <gengyangcai@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
[toc] | [prev] | [next] | [standalone]
| From | Cai Gengyang <gengyangcai@gmail.com> |
|---|---|
| Date | 2015-11-24 02:27 -0800 |
| Message-ID | <45b60b80-d6b7-423e-93b9-2ef4977fb368@googlegroups.com> |
| In reply to | #99306 |
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@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
[toc] | [prev] | [next] | [standalone]
| From | Peter Otten <__peter__@web.de> |
|---|---|
| Date | 2015-11-24 11:35 +0100 |
| Message-ID | <mailman.100.1448361611.2291.python-list@python.org> |
| In reply to | #99307 |
Cai Gengyang wrote:
> 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
Yeah, the error message really should be
"NameError: name 'result' is not defined; did you mean 'results'?"
;)
[toc] | [prev] | [next] | [standalone]
| From | Cai Gengyang <gengyangcai@gmail.com> |
|---|---|
| Date | 2015-11-24 02:47 -0800 |
| Message-ID | <16cbc004-c22f-492d-9f75-94e14374616d@googlegroups.com> |
| In reply to | #99309 |
Yup, thanks a lot ... it works now !
>>> results = {
"gengyang": 14,
"ensheng": 13,
"jordan": 12
}
>>> results["jordan"]
12
On Tuesday, November 24, 2015 at 6:40:26 PM UTC+8, Peter Otten wrote:
> Cai Gengyang wrote:
>
> > 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
>
> Yeah, the error message really should be
>
> "NameError: name 'result' is not defined; did you mean 'results'?"
>
> ;)
[toc] | [prev] | [next] | [standalone]
| From | Chris Angelico <rosuav@gmail.com> |
|---|---|
| Date | 2015-11-24 22:05 +1100 |
| Message-ID | <mailman.102.1448363112.2291.python-list@python.org> |
| In reply to | #99307 |
On Tue, Nov 24, 2015 at 9:35 PM, Peter Otten <__peter__@web.de> wrote: > Yeah, the error message really should be > > "NameError: name 'result' is not defined; did you mean 'results'?" > > ;) Heh. Yes, that was my fault. Sorry Cai! it should have been results["jordan"], as you figured out. ChrisA
[toc] | [prev] | [next] | [standalone]
| From | Cai Gengyang <gengyangcai@gmail.com> |
|---|---|
| Date | 2015-11-24 03:06 -0800 |
| Message-ID | <22e21276-50f6-49c8-925e-3d33284b49cb@googlegroups.com> |
| In reply to | #99313 |
Its cool ... No problem On Tuesday, November 24, 2015 at 7:05:25 PM UTC+8, Chris Angelico wrote: > On Tue, Nov 24, 2015 at 9:35 PM, Peter Otten <__peter__@web.de> wrote: > > Yeah, the error message really should be > > > > "NameError: name 'result' is not defined; did you mean 'results'?" > > > > ;) > > Heh. Yes, that was my fault. Sorry Cai! it should have been > results["jordan"], as you figured out. > > ChrisA
[toc] | [prev] | [next] | [standalone]
| From | Peter Otten <__peter__@web.de> |
|---|---|
| Date | 2015-11-24 11:32 +0100 |
| Message-ID | <mailman.99.1448361186.2291.python-list@python.org> |
| In reply to | #99305 |
Cai Gengyang 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 !
You can access the last item in a *list* with items[-1]:
>>> results = [
... ("gengyang", 14),
... ("ensheng", 13),
... ("jordan", 12)
... ]
>>> results[-1]
('jordan', 12)
A *dict* is unordered (the order is undefined, to be exact), so there is no
"last" item. For the rare case when you need both order and fast lookup
there's collections.OrderedDict:
>>> results = collections.OrderedDict([
... ("gengyang", 14),
... ("ensheng", 13),
... ("jordan", 12)
... ])
Get the last value non-destructively:
>>> results[next(reversed(results))] # is there a less tedious way?
12
Get the last pair, removing it from the dictionary:
>>> results.popitem(last=True)
('jordan', 12)
>>> "jordan" in results
False
[toc] | [prev] | [next] | [standalone]
| From | Denis McMahon <denismfmcmahon@gmail.com> |
|---|---|
| Date | 2015-11-24 14:07 +0000 |
| Message-ID | <n31qv3$4en$1@dont-email.me> |
| In reply to | #99305 |
On Tue, 24 Nov 2015 02:04:56 -0800, Cai Gengyang 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) ?
You open a web browser and google for "python dictionary"
--
Denis McMahon, denismfmcmahon@gmail.com
[toc] | [prev] | [next] | [standalone]
| From | Mark Lawrence <breamoreboy@yahoo.co.uk> |
|---|---|
| Date | 2015-11-24 14:28 +0000 |
| Message-ID | <mailman.119.1448375356.2291.python-list@python.org> |
| In reply to | #99335 |
On 24/11/2015 14:07, Denis McMahon wrote:
> On Tue, 24 Nov 2015 02:04:56 -0800, Cai Gengyang 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) ?
>
> You open a web browser and google for "python dictionary"
>
Ooh steady on old chap, surely you should have fitted the bib, done the
spoon feeding and then changed the nappy? That appears to me the
preferred way of doing things nowadays on c.l.py, rather than bluntly
telling people not to be so bloody lazy.
--
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 | Ned Batchelder <ned@nedbatchelder.com> |
|---|---|
| Date | 2015-11-24 08:59 -0800 |
| Message-ID | <1900b12c-eb55-4fc5-a45f-b134bf8d36db@googlegroups.com> |
| In reply to | #99341 |
On Tuesday, November 24, 2015 at 9:29:30 AM UTC-5, Mark Lawrence wrote:
> On 24/11/2015 14:07, Denis McMahon wrote:
> > On Tue, 24 Nov 2015 02:04:56 -0800, Cai Gengyang 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) ?
> >
> > You open a web browser and google for "python dictionary"
> >
>
> Ooh steady on old chap, surely you should have fitted the bib, done the
> spoon feeding and then changed the nappy? That appears to me the
> preferred way of doing things nowadays on c.l.py, rather than bluntly
> telling people not to be so bloody lazy.
Mark and Denis, if you are having a hard time with newcomers to this list,
perhaps you need to find another list to read?
Yes, Cai Gengyang could do more research before asking questions. But
your response does nothing to improve the situation. It's just snark
and bile, and does not exemplify the Python community's culture.
> My fellow Pythonistas, ask not what our language can do for you, ask
> what you can do for our language.
What you can do for our language is put a better foot forward.
--Ned.
[toc] | [prev] | [next] | [standalone]
| From | Cai Gengyang <gengyangcai@gmail.com> |
|---|---|
| Date | 2015-11-25 02:26 -0800 |
| Message-ID | <b110a0f7-557a-4da7-ba9b-54624fb1b7bc@googlegroups.com> |
| In reply to | #99369 |
Programming is indeed tough ... I wish I had picked up this skill much earlier in life.
But true, I will try to research my questions more in depth before posting here.
My goal eventually is to build a successful YCombinator based web-based startup.
On Wednesday, November 25, 2015 at 12:59:36 AM UTC+8, Ned Batchelder wrote:
> On Tuesday, November 24, 2015 at 9:29:30 AM UTC-5, Mark Lawrence wrote:
> > On 24/11/2015 14:07, Denis McMahon wrote:
> > > On Tue, 24 Nov 2015 02:04:56 -0800, Cai Gengyang 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) ?
> > >
> > > You open a web browser and google for "python dictionary"
> > >
> >
> > Ooh steady on old chap, surely you should have fitted the bib, done the
> > spoon feeding and then changed the nappy? That appears to me the
> > preferred way of doing things nowadays on c.l.py, rather than bluntly
> > telling people not to be so bloody lazy.
>
> Mark and Denis, if you are having a hard time with newcomers to this list,
> perhaps you need to find another list to read?
>
> Yes, Cai Gengyang could do more research before asking questions. But
> your response does nothing to improve the situation. It's just snark
> and bile, and does not exemplify the Python community's culture.
>
> > My fellow Pythonistas, ask not what our language can do for you, ask
> > what you can do for our language.
>
> What you can do for our language is put a better foot forward.
>
> --Ned.
[toc] | [prev] | [next] | [standalone]
| From | Mark Lawrence <breamoreboy@yahoo.co.uk> |
|---|---|
| Date | 2015-11-25 11:22 +0000 |
| Message-ID | <mailman.67.1448450556.20593.python-list@python.org> |
| In reply to | #99433 |
On 25/11/2015 10:26, Cai Gengyang wrote: > But true, I will try to research my questions more in depth before posting here. > Please also research how to intersperse your answers or bottom post. -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web