Re: [Tutor] selecting data from a list

2015-01-18 Thread Albert-Jan Roskam


- Original Message -

 From: Peter Otten __pete...@web.de
 To: tutor@python.org
 Cc: 
 Sent: Sunday, January 18, 2015 4:38 AM
 Subject: Re: [Tutor] selecting data from a list
 
 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.


If Peter is right about his hunch, then ignore the following. If not, then you 
could use a Boolean array to do the selection:

 import numpy as np
 arr = np.arange(11)
 arr
array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10])

 arr  5
array([False, False, False, False, False, False, True, True, True,
True, True], dtype=bool)

 arr[arr  5]
array([ 6,  7,  8,  9, 10])


Regards,
Albert-Jan
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] selecting data from a list

2015-01-18 Thread Mark Lawrence

On 18/01/2015 00:49, 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)):


As others have already offered the usual sound advice I'll just say that 
using the above type of construct is usually wrong in Python.  A useful 
tip is to make your container names plural, then process the singular 
name so lets have:-


cars = ['Ford, 'Vauxhall', 'Land Rover', 'Jaguar']
for car in cars:
doSomething(car)

--
My fellow Pythonistas, ask not what our language can do for you, ask
what you can do for our language.

Mark Lawrence

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


[Tutor] selecting data from a list

2015-01-17 Thread Colin Ross
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.

Thank you.

Colin
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] selecting data from a list

2015-01-17 Thread Alan Gauld

On 18/01/15 00:49, Colin Ross wrote:


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


You have named your iteration cvariable the same as the list you are 
iterating over. Don't ever do this!


In effect you have made your list invisible.
Python just sees:

for a in range(11):

and a becomes each integer in turn.
At the end of the loop a is the number 10.


a_1 = np.array(a)
print a_1


So now you try to create an array using just the number 10.


The desired result is: [6,7,8,9,10}


You could just rename the iteration variable:

for x in range(len(a)):
if x  5:
   print x

You would be better using a list comprehension:

a_1 = [x for x in range(len(a)) if x  5]
print a_1

or to create the array directly:

a_1 = array(x for x in range(len(a)) if x  5)

should work.

BTW I assume you will eventually want the contents
of the original list rather than the indexes?
If so it woyuld look like:

a_1 = array(a[x] for x in range(len(a)) if a[x]  5)

or, working on the list directly, and more generally:

a_1 = array(item for item in a if test(item) )

where test() is any filter function you care you write.



HTH
--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] selecting data from a list

2015-01-17 Thread Peter Otten
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.

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] selecting data from a list

2015-01-17 Thread Steven D'Aprano
Hi Colin, and welcome. My responses are interleaved with your comments 
below.


On Sat, Jan 17, 2015 at 08:49:30PM -0400, 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]

Your code below doesn't use either the list a or b. You create these 
lists, then (almost) immediately throw away a and don't use b at 
all. This makes is hard to tell precisely what you are attempting to do, 
since your description of what you want to do and what your code 
actually does are so very different, I'm having to guess what I imagine 
you probably want.


 for a in range(len(a)):
 if a  5:
 print a

That's a syntax error. Indentation is significant when programming in 
Python, so be careful to not lose it. That should be:

for a in range(len(a)):
if a  5:
print a

except it shouldn't because that is useless. All you are doing is 
printing out the matching numbers one at a time, then forgetting all 
about them.

In this case, we can write out what the Python interpreter will do, step 
by step:

* get the length of list a (in this case, 11)
* generate a new list range(11) -- [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
* re-assign the name a to the first item of this new list, 0
* which then allows the original list to be deleted and memory reclaimed
* check whether a is larger than 5
* since it isn't, continue with a = 1, a = 2, a = 3, a = 4, a = 5
* at last we get to a = 6
* which is larger than 5, so print 6
* continue with a = 7 (which is printed), a = 8, etc.
* finally the loop ends
* which leaves us with list b untouched and never used
* and a is set to 10


I'm going to guess what you intend instead:

* starting with list a = [0, 1, 2, 3, ... 9, 10]
* check each value to see if it is larger than 5
* if so, you want to REMEMBER THAT VALUE for later
* and collect all those values.

Here is the long way of doing that:


a = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
collector = []  # will hold the values we collect for later
for value in a:
if value  5:
collector.append(value)  # remember it for later
print Found, value

print collector
# optional: convert from a Python list to a numpy array
import numpy as np
a_1 = np.array(collector)
print a_1


Note that I make sure to avoid using the same name for the list a and 
the individual items inside a.



Here's a shorter way to do the same, using a list comprehension:

a = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
collector = [value for value in a if value  5]
a_1 = np.array(collector)
print a_1



And here's an even shorter way:

a_1 = np.array(range(6, 11))
print a_1


Can you work out what each of those three things are doing? Feel free to 
ask for help.



-- 
Steven
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor