[Tutor] selecting data from a list

Peter Otten __peter__ at web.de
Sun Jan 18 04:38:43 CET 2015


Colin Ross wrote:

> Hi all,
> 
> I am attempting to isolate a certain subset of data from the list "a" and
> then turn it into any array. To do so, I have written the following code:
> 
> import numpy as np
> 
> a = [0,1,2,3,4,5,6,7,8,9,10]
> b = [10,20,30,40,50,60,70,80,90,100,110]
> 
> for a in range(len(a)):
> if a > 5:
> print a
> 
> a_1 = np.array(a)
> 
> print a_1
> 
> The output is as follows:
> 
> 6
> 7
> 8
> 9
> 10
> 10
> 
> 
> As you can see, when I attempt to turn the list of numbers 6 through 10
> into an array I am left with it only printing out 10...
> 
> The desired result is: [6,7,8,9,10}
> 
> Any guidance would be greatly appreciated.

I have a hunch that you may be looking for slicing:

>>> a = [0,1,2,3,4,5,6,7,8,9,10]
>>> b = [10,20,30,40,50,60,70,80,90,100,110]
>>> a[6:]
[6, 7, 8, 9, 10]
>>> b[3:]
[40, 50, 60, 70, 80, 90, 100, 110]

If I'm right you should really work through a tutorial.



More information about the Tutor mailing list