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


Groups > comp.lang.python > #98861 > unrolled thread

What meaning is 'a[0:10:2]'?

Started byfl <rxjwg98@gmail.com>
First post2015-11-15 16:27 -0800
Last post2015-11-16 23:29 +1100
Articles 6 — 5 participants

Back to article view | Back to comp.lang.python


Contents

  What meaning is 'a[0:10:2]'? fl <rxjwg98@gmail.com> - 2015-11-15 16:27 -0800
    Re: What meaning is 'a[0:10:2]'? Ben Finney <ben+python@benfinney.id.au> - 2015-11-16 11:46 +1100
      Names [was Re: What meaning is 'a[0:10:2]'?] Steven D'Aprano <steve@pearwood.info> - 2015-11-16 23:57 +1100
        Re: Names [was Re: What meaning is 'a[0:10:2]'?] Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2015-11-16 20:06 -0500
    Re: What meaning is 'a[0:10:2]'? Tim Chase <python.list@tim.thechases.com> - 2015-11-15 18:48 -0600
    Re: What meaning is 'a[0:10:2]'? Steven D'Aprano <steve@pearwood.info> - 2015-11-16 23:29 +1100

#98861 — What meaning is 'a[0:10:2]'?

Fromfl <rxjwg98@gmail.com>
Date2015-11-15 16:27 -0800
SubjectWhat meaning is 'a[0:10:2]'?
Message-ID<237d9a48-6db3-48aa-89b4-66730cdced7d@googlegroups.com>
hi,

When I learn slice, I have a new question on the help file. If I set:

pp=a[0:10:2]

pp is array([1, 3])

I don't know how a[0:10:2] gives array([1, 3]).

I know matlab a lot, but here it seems quite different. Could you tell me
what meaning a[0:10:2] is?


Thanks,



class slice(object)
 |  slice(stop)
 |  slice(start, stop[, step])
 |  
 |  Create a slice object.  This is used for extended slicing (e.g. a[0:10:2]).

[toc] | [next] | [standalone]


#98862

FromBen Finney <ben+python@benfinney.id.au>
Date2015-11-16 11:46 +1100
Message-ID<mailman.354.1447634788.16136.python-list@python.org>
In reply to#98861
(Please set a “From” address that has a name for you as an individual;
“fl” is rather anonymous and doesn't help us to identify you in these
conversations.)

fl <rxjwg98@gmail.com> writes:

> When I learn slice

Are you working through the Python tutorial
<URL:https://docs.python.org/3/tutorial/>? These are Python concepts
that you will learn by working through the tutorial, from beginning to
end.

If you want to engage with a community dedicated to teaching Python
newcomers, you should join the Python ‘tutor’ forum
<URL:https://mail.python.org/mailman/listinfo/tutor>.

-- 
 \       “Philosophy is questions that may never be answered. Religion |
  `\              is answers that may never be questioned.” —anonymous |
_o__)                                                                  |
Ben Finney

[toc] | [prev] | [next] | [standalone]


#98877 — Names [was Re: What meaning is 'a[0:10:2]'?]

FromSteven D'Aprano <steve@pearwood.info>
Date2015-11-16 23:57 +1100
SubjectNames [was Re: What meaning is 'a[0:10:2]'?]
Message-ID<5649d2bd$0$1613$c3e8da3$5496439d@news.astraweb.com>
In reply to#98862
On Mon, 16 Nov 2015 11:46 am, Ben Finney wrote:

> (Please set a “From” address that has a name for you as an individual;
> “fl” is rather anonymous and doesn't help us to identify you in these
> conversations.)

As far as I know, there's only one "fl" who posts here. And "fl" as a
cognomen is no worse than such well-known examples as:

J.R. https://en.wikipedia.org/wiki/JR_(artist)

and https://en.wikipedia.org/wiki/Who_shot_J.R.%3F

M.J. https://en.wikipedia.org/wiki/Mary_Jane_Watson

djk (who you worked with for a number of years *wink*)

T.I. https://en.wikipedia.org/wiki/T.I.

not to mention many famous people who are frequently, but not exclusively,
known by initials:

JMS https://en.wikipedia.org/wiki/J._Michael_Straczynski
MLK https://en.wikipedia.org/wiki/Martin_Luther_King,_Jr.
JFK https://en.wikipedia.org/wiki/John_F._Kennedy


On-the-internet-nobody-can-tell-if-your-name-really-is-Mxyzptlk-ly yr's,


-- 
Steven

[toc] | [prev] | [next] | [standalone]


#98905 — Re: Names [was Re: What meaning is 'a[0:10:2]'?]

FromDennis Lee Bieber <wlfraed@ix.netcom.com>
Date2015-11-16 20:06 -0500
SubjectRe: Names [was Re: What meaning is 'a[0:10:2]'?]
Message-ID<mailman.375.1447722608.16136.python-list@python.org>
In reply to#98877
On Mon, 16 Nov 2015 23:57:31 +1100, Steven D'Aprano <steve@pearwood.info>
declaimed the following:

>On-the-internet-nobody-can-tell-if-your-name-really-is-Mxyzptlk-ly yr's,
>
	But it's a lot harder to be tricked into speaking it in reverse...
-- 
	Wulfraed                 Dennis Lee Bieber         AF6VN
    wlfraed@ix.netcom.com    HTTP://wlfraed.home.netcom.com/

[toc] | [prev] | [next] | [standalone]


#98863

FromTim Chase <python.list@tim.thechases.com>
Date2015-11-15 18:48 -0600
Message-ID<mailman.355.1447634986.16136.python-list@python.org>
In reply to#98861
On 2015-11-15 16:27, fl wrote:
> When I learn slice, I have a new question on the help file. If I
> set:
> 
> pp=a[0:10:2]
> 
> pp is array([1, 3])
> 
> I don't know how a[0:10:2] gives array([1, 3]).
> 
> I know matlab a lot, but here it seems quite different. Could you
> tell me what meaning a[0:10:2] is?

Well, if it a matlab.array was a well-behaved object it would just
give you "0".  As your copy/paste on the help for a slice stated, the
first number is where it starts (0), the second number is where it
ends (10, exclusive), and the 3rd number (2) is the stride.  To
demonstrate:

  >>> a = list(range(20))
  >>> a
  [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
  >>> a[0:10:2]
  [0, 2, 4, 6, 8]
  >>> a[:10:2]
  [0, 2, 4, 6, 8]
  >>> a[0:10]
  [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

So note that the stride of 2 provides every other one while a stride
of three provides every 3rd value

  >>> a[0:10:3]
  [0, 3, 6, 9]

-tkc

[toc] | [prev] | [next] | [standalone]


#98875

FromSteven D'Aprano <steve@pearwood.info>
Date2015-11-16 23:29 +1100
Message-ID<5649cc16$0$1592$c3e8da3$5496439d@news.astraweb.com>
In reply to#98861
On Mon, 16 Nov 2015 11:27 am, fl wrote:

> hi,
> 
> When I learn slice, I have a new question on the help file. If I set:
> 
> pp=a[0:10:2]
> 
> pp is array([1, 3])

Really? How do you get that answer? What is `a`?


> I don't know how a[0:10:2] gives array([1, 3]).

Neither do I, because you have not told us what `a` is.

But I know what `a[0:10:2]` *should* be:

it takes a copy of elements from a, starting at position 0, ending just
before position 10, and taking every second one.


py> a = list(range(100, 121))
py> print(a)
[100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114,
115, 116, 117, 118, 119, 120]

py> print(a[0:10])
[100, 101, 102, 103, 104, 105, 106, 107, 108, 109]

py> print(a[0:10:2])
[100, 102, 104, 106, 108]

py> print(a[0:10:3])
[100, 103, 106, 109]


By default, the first item is automatically 0, so these two slices are the
same:

a[0:10:2]
a[:10:2]


If the start or end position are out of range, the slice will only include
positions that actually exist:

py> a[:10000:5]
[100, 105, 110, 115, 120]




-- 
Steven

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web