Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Tim Chase 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: 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: 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 List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Xref: csiph.com comp.lang.python:98863 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