Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #91826 > unrolled thread
| Started by | fl <rxjwg98@gmail.com> |
|---|---|
| First post | 2015-06-02 05:23 -0700 |
| Last post | 2015-06-03 01:11 +1000 |
| Articles | 10 — 7 participants |
Back to article view | Back to comp.lang.python
How to access the low digits of a list fl <rxjwg98@gmail.com> - 2015-06-02 05:23 -0700
Re: How to access the low digits of a list Joel Goldstick <joel.goldstick@gmail.com> - 2015-06-02 08:32 -0400
Re: How to access the low digits of a list Frank Stutzman <stutzman@cat2.kjsl.com> - 2015-06-02 12:34 +0000
Re: How to access the low digits of a list Rustom Mody <rustompmody@gmail.com> - 2015-06-02 05:35 -0700
Re: How to access the low digits of a list Ian Kelly <ian.g.kelly@gmail.com> - 2015-06-02 08:19 -0600
Re: How to access the low digits of a list Rustom Mody <rustompmody@gmail.com> - 2015-06-03 14:08 -0700
Re: How to access the low digits of a list Mark Lawrence <breamoreboy@yahoo.co.uk> - 2015-06-03 22:41 +0100
Re: How to access the low digits of a list Ian Kelly <ian.g.kelly@gmail.com> - 2015-06-03 16:36 -0600
Re: How to access the low digits of a list Steven D'Aprano <steve@pearwood.info> - 2015-06-04 23:27 +1000
Re: How to access the low digits of a list Steven D'Aprano <steve@pearwood.info> - 2015-06-03 01:11 +1000
| From | fl <rxjwg98@gmail.com> |
|---|---|
| Date | 2015-06-02 05:23 -0700 |
| Subject | How to access the low digits of a list |
| Message-ID | <efe9d48e-9aaf-4e68-aa26-c83da32e80f5@googlegroups.com> |
Hi, I have a list: >>> lines ['12', '42', '49', '156', '225', '36', '49', '164', '11181', '3100'] I want to access the last two digits. That is: ['12', '42', '49', '56', '25', '36', '49', '64', '81', '00'] When I try to use lines[3][0] is '1' lines[3][1] is '5' lines[3][2] is '6' I don't know whether there is a way to know the length of lines[3]. Then, I can use a -1 step to get the last two digits. Or, you may have much better ways to do that. Python is really too versatile I feel. Thanks,
[toc] | [next] | [standalone]
| From | Joel Goldstick <joel.goldstick@gmail.com> |
|---|---|
| Date | 2015-06-02 08:32 -0400 |
| Message-ID | <mailman.55.1433248322.13271.python-list@python.org> |
| In reply to | #91826 |
On Tue, Jun 2, 2015 at 8:23 AM, fl <rxjwg98@gmail.com> wrote: > Hi, > > I have a list: > > > > > > >>>> lines > ['12', '42', '49', '156', '225', '36', '49', '164', '11181', '3100'] > > > > I want to access the last two digits. That is: > > ['12', '42', '49', '56', '25', '36', '49', '64', '81', '00'] > > > When I try to use lines[3][0] is '1' > lines[3][1] is '5' > lines[3][2] is '6' > > > I don't know whether there is a way to know the length of lines[3]. Then, I > can use a -1 step to get the last two digits. Or, you may have much better > ways to do that. Python is really too versatile I feel. > > Thanks, > -- > https://mail.python.org/mailman/listinfo/python-list You should read about slices lines[3][-2:] will give the last two characters in the 3rd group (starting from 0) The notation means to start with 2 from the end, and proceed to the end -- Joel Goldstick http://joelgoldstick.com
[toc] | [prev] | [next] | [standalone]
| From | Frank Stutzman <stutzman@cat2.kjsl.com> |
|---|---|
| Date | 2015-06-02 12:34 +0000 |
| Message-ID | <mkk7s6$1iut$1@news.kjsl.com> |
| In reply to | #91826 |
fl <rxjwg98@gmail.com> wrote: ---snip---- >>>> lines > ['12', '42', '49', '156', '225', '36', '49', '164', '11181', '3100'] > > > > I want to access the last two digits. That is: > > ['12', '42', '49', '56', '25', '36', '49', '64', '81', '00'] > > > When I try to use lines[3][0] is '1' > lines[3][1] is '5' > lines[3][2] is '6' Is there something wrong with using: lines[3][-2:] -- Frank Stutzman
[toc] | [prev] | [next] | [standalone]
| From | Rustom Mody <rustompmody@gmail.com> |
|---|---|
| Date | 2015-06-02 05:35 -0700 |
| Message-ID | <18a4709f-47c8-41f7-87d5-08bbb00166b6@googlegroups.com> |
| In reply to | #91826 |
On Tuesday, June 2, 2015 at 5:53:34 PM UTC+5:30, fl wrote: > Hi, > > I have a list: > > > > > > > >>> lines > ['12', '42', '49', '156', '225', '36', '49', '164', '11181', '3100'] > > > > I want to access the last two digits. That is: > > ['12', '42', '49', '56', '25', '36', '49', '64', '81', '00'] > > > When I try to use lines[3][0] is '1' > lines[3][1] is '5' > lines[3][2] is '6' > > > I don't know whether there is a way to know the length of lines[3]. Then, I > can use a -1 step to get the last two digits. Or, you may have much better > ways to do that. Python is really too versatile I feel. len(lines[3]) ?? You can do this: [Hope I am not doing your homework!] >>> [(x[-2:] if len(x) > 2 else x) for x in lines] ['12', '42', '49', '56', '25', '36', '49', '64', '81', '00'] >>> For that matter even this works But I am not sure whats happening or that I like it >>> [x[-2:] for x in lines] ['12', '42', '49', '56', '25', '36', '49', '64', '81', '00'] >>>
[toc] | [prev] | [next] | [standalone]
| From | Ian Kelly <ian.g.kelly@gmail.com> |
|---|---|
| Date | 2015-06-02 08:19 -0600 |
| Message-ID | <mailman.63.1433254823.13271.python-list@python.org> |
| In reply to | #91830 |
On Tue, Jun 2, 2015 at 6:35 AM, Rustom Mody <rustompmody@gmail.com> wrote: > For that matter even this works > But I am not sure whats happening or that I like it > >>>> [x[-2:] for x in lines] > ['12', '42', '49', '56', '25', '36', '49', '64', '81', '00'] x[-2:] selects all items in the sequence with index i such that len(x) - 2 <= i < len(x). For a sequence of length 2 or less, that's the entire sequence.
[toc] | [prev] | [next] | [standalone]
| From | Rustom Mody <rustompmody@gmail.com> |
|---|---|
| Date | 2015-06-03 14:08 -0700 |
| Message-ID | <11005554-e89f-407c-8720-fae1bb3fced8@googlegroups.com> |
| In reply to | #91843 |
On Tuesday, June 2, 2015 at 7:50:58 PM UTC+5:30, Ian wrote: > On Tue, Jun 2, 2015 at 6:35 AM, Rustom Mody wrote: > > For that matter even this works > > But I am not sure whats happening or that I like it > > > >>>> [x[-2:] for x in lines] > > ['12', '42', '49', '56', '25', '36', '49', '64', '81', '00'] > > x[-2:] selects all items in the sequence with index i such that len(x) > - 2 <= i < len(x). For a sequence of length 2 or less, that's the > entire sequence. Thanks -- learn something So it means that indices can give indexerror; slices cannot? Seems fair enough put that way, but is visually counterintuitive
[toc] | [prev] | [next] | [standalone]
| From | Mark Lawrence <breamoreboy@yahoo.co.uk> |
|---|---|
| Date | 2015-06-03 22:41 +0100 |
| Message-ID | <mailman.130.1433367715.13271.python-list@python.org> |
| In reply to | #91996 |
On 03/06/2015 22:08, Rustom Mody wrote: > On Tuesday, June 2, 2015 at 7:50:58 PM UTC+5:30, Ian wrote: >> On Tue, Jun 2, 2015 at 6:35 AM, Rustom Mody wrote: >>> For that matter even this works >>> But I am not sure whats happening or that I like it >>> >>>>>> [x[-2:] for x in lines] >>> ['12', '42', '49', '56', '25', '36', '49', '64', '81', '00'] >> >> x[-2:] selects all items in the sequence with index i such that len(x) >> - 2 <= i < len(x). For a sequence of length 2 or less, that's the >> entire sequence. > > Thanks -- learn something > So it means that indices can give indexerror; slices cannot? > Seems fair enough put that way, but is visually counterintuitive > Are you seriously trying to say that you teach Python but don't understand a basic that is here https://docs.python.org/3/tutorial/introduction.html, "Slice indices have useful defaults; an omitted first index defaults to zero, an omitted second index defaults to the size of the string being sliced."? -- 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 | Ian Kelly <ian.g.kelly@gmail.com> |
|---|---|
| Date | 2015-06-03 16:36 -0600 |
| Message-ID | <mailman.137.1433371024.13271.python-list@python.org> |
| In reply to | #91996 |
On Wed, Jun 3, 2015 at 3:08 PM, Rustom Mody <rustompmody@gmail.com> wrote: > On Tuesday, June 2, 2015 at 7:50:58 PM UTC+5:30, Ian wrote: >> On Tue, Jun 2, 2015 at 6:35 AM, Rustom Mody wrote: >> > For that matter even this works >> > But I am not sure whats happening or that I like it >> > >> >>>> [x[-2:] for x in lines] >> > ['12', '42', '49', '56', '25', '36', '49', '64', '81', '00'] >> >> x[-2:] selects all items in the sequence with index i such that len(x) >> - 2 <= i < len(x). For a sequence of length 2 or less, that's the >> entire sequence. > > Thanks -- learn something > So it means that indices can give indexerror; slices cannot? > Seems fair enough put that way, but is visually counterintuitive Yes. The rule I paraphrased above is stated at https://docs.python.org/3/library/stdtypes.html#common-sequence-operations -- scroll down to note 4. I don't know if there's anything that clearly states that sequence slicing can't raise IndexError, but it is at least implied by the above, and it is certainly true of all builtin sequence types.
[toc] | [prev] | [next] | [standalone]
| From | Steven D'Aprano <steve@pearwood.info> |
|---|---|
| Date | 2015-06-04 23:27 +1000 |
| Message-ID | <55705246$0$12991$c3e8da3$5496439d@news.astraweb.com> |
| In reply to | #91996 |
On Thu, 4 Jun 2015 07:08 am, Rustom Mody wrote: > So it means that indices can give indexerror; slices cannot? If you write your own class with a __getitem__ method, you can have it do anything you like, including raise an exception. Built-in sequence types like list, str and tuple, however, allow slicing to extend beyond the first and last indexes without error. -- Steven
[toc] | [prev] | [next] | [standalone]
| From | Steven D'Aprano <steve@pearwood.info> |
|---|---|
| Date | 2015-06-03 01:11 +1000 |
| Message-ID | <556dc7a0$0$13009$c3e8da3$5496439d@news.astraweb.com> |
| In reply to | #91826 |
On Tue, 2 Jun 2015 10:23 pm, fl wrote:
> I don't know whether there is a way to know the length of lines[3].
The same way as to know the length of anything else:
len(x) # the length of x
len("hello") # the length of "hello"
len(lines[3]) # the length of lines[3]
> Then,
> I can use a -1 step to get the last two digits. Or, you may have much
> better ways to do that. Python is really too versatile I feel.
Last two digits of a string:
mystring = "123456"
mystring[-2:]
--
Steven
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web