2

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([])

desertnaut
  • 57,590
  • 26
  • 140
  • 166

6 Answers6

3

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]])
Graipher
  • 6,891
  • 27
  • 47
2

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)
Mihai Alexandru-Ionut
  • 47,092
  • 13
  • 101
  • 128
  • Yeah i needed exactly that. But i actually forgot to mention that i am using numpy.array([]). So it's an object so i can't do this operation. Sorry for not being precise im noob in python. – Kamil Septio Trojnar Mar 19 '18 at 10:41
  • 1
    Doesn't seem like the most efficient thing to do in OP's case (numpy). I suggest using [`np.where`](https://docs.scipy.org/doc/numpy/reference/generated/numpy.where.html) instead – ninesalt Mar 19 '18 at 10:53
2

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]]
vvvvv
  • 25,404
  • 19
  • 49
  • 81
1

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]
Rahul
  • 10,830
  • 4
  • 53
  • 88
1

Try this:

new_list = []
for i in list:
    has_zero = i[1]
    if has_zero==0:
        new_list.append(i)
print(new_list)
Narendra
  • 1,511
  • 1
  • 10
  • 20
1

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]]
Vader
  • 3,675
  • 23
  • 40