2

How to get first element from each dimension in a numpy ndarray?

import numpy
A = numpy.array([['a','b','c'],['d','e','f'],['g','h','i']])

Result should be:

Result = ['a','d','g']

3 Answers 3

4
>>> import numpy
>>> A = numpy.array([['a','b','c'],['d','e','f'],['g','h','i']])
>>> A[:,0]
array(['a', 'd', 'g'],
      dtype='|S1')
>>> A[...,0]
array(['a', 'd', 'g'],
      dtype='|S1')

See Indexing (basic) - NumPy Manual , Indexing - NumPy Manual.

Sign up to request clarification or add additional context in comments.

Comments

2

Did you try this?

  list( A[:,0] )

Indexing a numpy array will normally return another Numpy array, so you will need the list constructor if you need a list.

1 Comment

please explain the meanings of A[:,0] @dsign
1

use the take function

import numpy
A = numpy.array([['a','b','c'],['d','e','f'],['g','h','i']])
print A.take((0,), 1)

2 Comments

what are the meanings of 0 and 1 in A.take?@lcfseth
it means that you want to get the first element ((0,)) (you can specify that you want the first and the third for example) along the (1) first axis.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.