1

I have a tuple like this:

(array([0, 0, 0]), array([0, 1, 2]))

I would like to select the first element of each array in this tuple and want to achieve this result: [0,0] or [0,1] or [0,2]

How can I do this?

Actually, this tuple is obtained from a function in numpy.where what I do is like this:

import numpy as np

x=np.array([[1,2,3],[4,5,6],[7,8,9]])

xi=np.where(x<4)

any suggestion?

Community
  • 1
  • 1
Jen
  • 339
  • 1
  • 2
  • 12

2 Answers2

0

Is this what you want ?

list(zip(tp[0],tp[1]))
Out[689]: [(0, 0), (0, 1), (0, 2)]

Data input

tp=(np.array([0, 0, 0]), np.array([0, 1, 2]))

If you want to list of list

l=list(zip(tp[0],tp[1]))
list(map(list, l))
Out[691]: [[0, 0], [0, 1], [0, 2]]
BENY
  • 317,841
  • 20
  • 164
  • 234
0

You can iterate over the numpy array through nditer and pair them:

np.array([(x,y) for x,y in np.nditer(xi)]) 

array([[0, 0],
   [0, 1],
   [0, 2]], dtype=int32)
skrubber
  • 1,095
  • 1
  • 9
  • 18