简体   繁体   English

从numpy ndarray中的每个维度获取某些元素

[英]get certain element from each dimension in a numpy ndarray

How to get first element from each dimension in a numpy ndarray? 如何从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 . 请参阅索引(基本) - NumPy手册索引 - NumPy手册

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. 索引一个numpy的阵列,通常会返回另一个数组numpy的,所以你需要在列表构造函数,如果你需要一个列表。

use the take function 使用汇整功能

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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