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']
>>> 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 .
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.
use the take function
import numpy
A = numpy.array([['a','b','c'],['d','e','f'],['g','h','i']])
print A.take((0,), 1)
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.