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


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

How to index an array with even steps?

Started byfl <rxjwg98@gmail.com>
First post2014-07-25 04:45 -0700
Last post2014-07-25 16:02 +0400
Articles 4 — 3 participants

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


Contents

  How to index an array with even steps? fl <rxjwg98@gmail.com> - 2014-07-25 04:45 -0700
    Re: How to index an array with even steps? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-07-25 11:55 +0000
    Re: How to index an array with even steps? fl <rxjwg98@gmail.com> - 2014-07-25 04:56 -0700
    Re: How to index an array with even steps? Akira Li <4kir4.1i@gmail.com> - 2014-07-25 16:02 +0400

#75199 — How to index an array with even steps?

Fromfl <rxjwg98@gmail.com>
Date2014-07-25 04:45 -0700
SubjectHow to index an array with even steps?
Message-ID<9160a3c5-ff5f-4fb2-a125-4a1be28438b9@googlegroups.com>
Hi,

I have an array arr which is indexed from 0 to 999. I would like to construct a 
column in two steps. The first step is input from 200 data, evenly spread from 0
to 999 of the target array. Then, I want to use interpolate it from 200 to 1000
with interpolate method.

In Python, ':' is used to indicate range (while in Matlab I know it can be used
to control steps). How to index an array with 0, 5, 10, 15...995?


Thanks,

[toc] | [next] | [standalone]


#75200

FromSteven D'Aprano <steve+comp.lang.python@pearwood.info>
Date2014-07-25 11:55 +0000
Message-ID<53d245bb$0$29966$c3e8da3$5496439d@news.astraweb.com>
In reply to#75199
On Fri, 25 Jul 2014 04:45:31 -0700, fl wrote:

> Hi,
> 
> I have an array arr which is indexed from 0 to 999. I would like to
> construct a column in two steps. The first step is input from 200 data,
> evenly spread from 0 to 999 of the target array. Then, I want to use
> interpolate it from 200 to 1000 with interpolate method.
> 
> In Python, ':' is used to indicate range (while in Matlab I know it can
> be used to control steps). How to index an array with 0, 5, 10,
> 15...995?

You can take a slice of the array and specify the step size. This works 
with lists, tuples, arrays and strings. For convenience, I will use 
strings, since there is less typing:

py> data = "abcdefghijklmnopqrstuvwxyz"
py> data[::5]
'afkpuz'
py> data[::3]
'adgjmpsvy'


You can specify a slice using any combination of:

    start : end : step


If you leave the start out, it defaults to 0, if you leave the end out, 
it defaults to the end of the string or list, and if you leave the step 
out, it defaults to 1.


Similarly, range() takes up to three arguments:

range(end)
range(start, end)
range(start, end, step)



-- 
Steven

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


#75201

Fromfl <rxjwg98@gmail.com>
Date2014-07-25 04:56 -0700
Message-ID<460a5ed0-df63-4018-bcc9-c266daca525b@googlegroups.com>
In reply to#75199
On Friday, July 25, 2014 7:45:31 AM UTC-4, fl wrote:
> to 999 of the target array. Then, I want to use interpolate it from 200 to 1000
> 
> with interpolate method.
> 
> In Python, ':' is used to indicate range (while in Matlab I know it can be used
> to control steps). How to index an array with 0, 5, 10, 15...995?
> 
> Thanks,

Sorry, I got it.
 x = np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
>>> x[1:7:2]
array([1, 3, 5])

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


#75202

FromAkira Li <4kir4.1i@gmail.com>
Date2014-07-25 16:02 +0400
Message-ID<mailman.12314.1406289784.18130.python-list@python.org>
In reply to#75199
fl <rxjwg98@gmail.com> writes:
> In Python, ':' is used to indicate range (while in Matlab I know it can be used
> to control steps). How to index an array with 0, 5, 10, 15...995?

Just use slicing:

  >>> L = range(1000) # your array goes here
  >>> L[::5] 
  [0, 5, 10, 15, ..., 995] # Python 2
  range(0, 1000, 5)        # Python 3


--
Akira

[toc] | [prev] | [standalone]


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


csiph-web