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


Groups > comp.lang.python > #98863

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

Path csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail
From Tim Chase <python.list@tim.thechases.com>
Newsgroups comp.lang.python
Subject Re: What meaning is 'a[0:10:2]'?
Date Sun, 15 Nov 2015 18:48:04 -0600
Lines 38
Message-ID <mailman.355.1447634986.16136.python-list@python.org> (permalink)
References <237d9a48-6db3-48aa-89b4-66730cdced7d@googlegroups.com>
Mime-Version 1.0
Content-Type text/plain; charset=US-ASCII
Content-Transfer-Encoding 7bit
X-Trace news.uni-berlin.de kW42qjiwEjIj0EHOtdCbjgGknRWp7qPX6hTY4eKWhsEQ==
Return-Path <python.list@tim.thechases.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.018
X-Spam-Evidence '*H*': 0.96; '*S*': 0.00; '16,': 0.03; '[0,': 0.09; '-tkc': 0.16; 'from:addr:python.list': 0.16; 'from:addr:tim.thechases.com': 0.16; 'from:name:tim chase': 0.16; 'matlab': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'set:': 0.16; 'slice,': 0.16; 'stated,': 0.16; 'wrote:': 0.16; '>>>': 0.20; 'file.': 0.22; 'seems': 0.23; 'second': 0.24; 'header :In-Reply-To:1': 0.24; 'question': 0.27; '14,': 0.27; '13,': 0.29; 'starts': 0.29; '15,': 0.30; 'is?': 0.33; 'gives': 0.35; 'could': 0.35; 'quite': 0.35; 'but': 0.36; 'to:addr:python-list': 0.36; 'subject:: ': 0.37; 'received:10': 0.37; '(2)': 0.37; '12,': 0.37; 'charset:us-ascii': 0.37; 'subject:[': 0.39; 'to:addr:python.org': 0.40; 'where': 0.40; 'your': 0.60; 'here': 0.66; 'received:23': 0.84; 'subject::': 0.85; 'different.': 0.91; 'lot,': 0.95
X-Sender-Id wwwh|x-authuser|tim@thechases.com
X-Sender-Id wwwh|x-authuser|tim@thechases.com
X-MC-Relay Neutral
X-MailChannels-SenderId wwwh|x-authuser|tim@thechases.com
X-MailChannels-Auth-Id wwwh
X-MC-Loop-Signature 1447634974882:4152909346
X-MC-Ingress-Time 1447634974882
In-Reply-To <237d9a48-6db3-48aa-89b4-66730cdced7d@googlegroups.com>
X-Mailer Claws Mail 3.11.1 (GTK+ 2.24.25; x86_64-pc-linux-gnu)
X-AuthUser tim@thechases.com
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:98863

Show key headers only | View raw


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

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

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

csiph-web