[英]How to get a specific array position after numpy.where condition?
I am using the where condition to my array and get back the array position array_string[0,0] with 'A' but I need the position array_string[0,1] with 200.我对我的数组使用 where 条件并取回数组 position array_string[0,0] 和 'A' 但我需要 position array_string[0,1] 和 200。
array_string = np.array([['A', 200],['B', 100]])
array_string[ np.where( array_string == 'A' ) ]
First of all, there is no need to use the np.where
.首先,不需要使用
np.where
。 Secondly, you can get your desired answer like this:其次,你可以像这样得到你想要的答案:
array_string[array_string[0,:]=='A', :]
> array([['A', '200']], dtype='<U3')
Translated into human language: check the first row in all columns, and if it is equal to A, get me the entire row (all the columns of this row).翻译成人类语言:检查所有列中的第一行,如果等于A,则给我整行(该行的所有列)。
This happens, because np.where
return indices for each dimension where element was found.发生这种情况是因为
np.where
返回找到元素的每个维度的索引。
array_string = np.array([['A', 200], ['B', 100]])
rows_found, cols_found = np.where(array_string == 'A')
rows = array_string[rows_found] # returns [['A', 200]]
print(rows[0][1]) # prints 200
# Another example
array_string = np.array([['A', 200], ['B', 100], ['A', 500]])
rows_found, cols_found = np.where(array_string == 'A')
rows = array_string[rows_found] # returns [['A' 200], ['A' 500]]
print(rows[:, 1]) # prints [200, 500]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.