[Tutor] selecting data from a list

Steven D'Aprano steve at pearwood.info
Sun Jan 18 02:25:19 CET 2015


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


More information about the Tutor mailing list