简体   繁体   中英

Selecting arrays in python under conditions

I have this array:

[[0, 1, 0, 1, 0, 1],
 [0, 0, 0, 1, 0, 0],
 [0, 0, 0, 0, 1, 0],
 [1, 0, 1, 0, 1, 0],
 [0, 1, 1, 1, 0, 1],
 [0, 1, 0, 0, 1, 1],
 [1, 1, 1, 0, 0, 0],
 [1, 1, 1, 1, 0, 1],
 [0, 1, 1, 0, 1, 0],
 [1, 1, 0, 0, 0, 1],
 [1, 0, 0, 0, 1, 0]]

I wish to create a new array, that will be only the rows that has 0 in column 1.

How can I build such array in python without writing up the function by myself. I have tried too complicated stuff and I just need simple selection method that gives me result.

@EDIT I forgot to mention that i am using numpy.array([])

Since you are saying that you are using numpy , this is a good place for numpy.where :

import numpy as np

a = np.array([[0, 1, 0, 1, 0, 1], [0, 0, 0, 1, 0, 0], [0, 0, 0, 0, 1, 0], [1, 0, 1, 0, 1, 0], [0, 1, 1, 1, 0, 1], [0, 1, 0, 0, 1, 1], [1, 1, 1, 0, 0, 0], [1, 1, 1, 1, 0, 1], [0, 1, 1, 0, 1, 0], [1, 1, 0, 0, 0, 1], [1, 0, 0, 0, 1, 0]])
a_new = a[np.where(a[:,1] == 0)]
print(a_new)
# array([[0, 0, 0, 1, 0, 0],
#        [0, 0, 0, 0, 1, 0],
#        [1, 0, 1, 0, 1, 0],
#        [1, 0, 0, 0, 1, 0]])

You can use list comprehensions

list = [[0, 1, 0, 1, 0, 1],[0, 0, 0, 1, 0, 0],[0, 0, 0, 0, 1, 0],[1, 0, 1, 0, 1, 0],[0, 1, 1, 1, 0, 1],[0, 1, 0, 0, 1, 1],[1, 1, 1, 0, 0, 0],[1, 1, 1, 1, 0, 1],[0, 1, 1, 0, 1, 0],[1, 1, 0, 0, 0, 1],[1, 0, 0, 0, 1, 0]]
list = [item for item in list if item[1] == 0]

Output:

[[0, 0, 0, 1, 0, 0], [0, 0, 0, 0, 1, 0], [1, 0, 1, 0, 1, 0], [1, 0, 0, 0, 1, 0]]

If you're using numpy array, the first step is converting your numpy array to list using tolist method.

import numpy
array = numpy.array([[0, 1, 0, 1, 0, 1],[0, 0, 0, 1, 0, 0],[0, 0, 0, 0, 1, 0],[1, 0, 1, 0, 1, 0],[0, 1, 1, 1, 0, 1],[0, 1, 0, 0, 1, 1],[1, 1, 1, 0, 0, 0],[1, 1, 1, 1, 0, 1],[0, 1, 1, 0, 1, 0],[1, 1, 0, 0, 0, 1],[1, 0, 0, 0, 1, 0]])
list = [item for item in array.tolist() if item[1] == 0]
array = numpy.array(list)

You can do it this way:

a = [[0, 1, 0, 1, 0, 1],[0, 0, 0, 1, 0, 0],[0, 0, 0, 0, 1, 0],[1, 0, 1, 0, 1, 0],[0, 1, 1, 1, 0, 1],[0, 1, 0, 0, 1, 1],[1, 1, 1, 0, 0, 0],[1, 1, 1, 1, 0, 1],[0, 1, 1, 0, 1, 0],[1, 1, 0, 0, 0, 1],[1, 0, 0, 0, 1, 0]]

b = [l for l in a if len(l) > 1 and l[1] == 0]
print(b)

The output would be:

[[0, 0, 0, 1, 0, 0], [0, 0, 0, 0, 1, 0], [1, 0, 1, 0, 1, 0], [1, 0, 0, 0, 1, 0]]

You should use list comp

l = [[0, 1, 0, 1, 0, 1],[0, 0, 0, 1, 0, 0],[0, 0, 0, 0, 1, 0],[1, 0, 1, 0, 1, 0],[0, 1, 1, 1, 0, 1],[0, 1, 0, 0, 1, 1],[1, 1, 1, 0, 0, 0],[1, 1, 1, 1, 0, 1],[0, 1, 1, 0, 1, 0],[1, 1, 0, 0, 0, 1],[1, 0, 0, 0, 1, 0]]
l1 = [item for item in l if item[1] == 0]

Try this:

new_list = []
for i in list:
    has_zero = i[1]
    if has_zero==0:
        new_list.append(i)
print(new_list)

This can be done with list comprehensions as suggested in other answers as well as with filter that might be bit clearer semantically in your case:

>>> a = [[0, 1, 0, 1, 0, 1],
...  [0, 0, 0, 1, 0, 0],
...  [0, 0, 0, 0, 1, 0],
...  [1, 0, 1, 0, 1, 0],
...  [0, 1, 1, 1, 0, 1],
...  [0, 1, 0, 0, 1, 1],
...  [1, 1, 1, 0, 0, 0],
...  [1, 1, 1, 1, 0, 1],
...  [0, 1, 1, 0, 1, 0],
...  [1, 1, 0, 0, 0, 1],
...  [1, 0, 0, 0, 1, 0]]
>>> filter(lambda row: row[1] == 0, a)
[[0, 0, 0, 1, 0, 0], [0, 0, 0, 0, 1, 0], [1, 0, 1, 0, 1, 0], [1, 0, 0, 0, 1, 0]]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM