如何解释“ np.where”的结果?

时间:2019-11-09 15:12:31

标签: python arrays numpy

我是Numpy的新手,并且没有扎实的基础。我目前正在尝试确定数组每一行中的最大整数,该整数在数组中的位置的行和列并打印出来。

但是,当尝试打印出最大整数元素的行和列时,我意识到输出会继续用“(array ..)”打印出元素的位置。无论如何,我可以从输出中删除(array...)吗?

import numpy as np

a = np.random.randint(1,1000,(3,4))

#MAX A(s)
maxa = a.max(axis = 1)
arra1 = maxa[np.asarray(0)]
arra2 = maxa[np.asarray(1)]
arra3 = maxa[np.asarray(2)]

#LOCATION OF MAX A(s)
arra1loc = np.where(a == arra1)

print('*** Contents of array a ***')
print(a)
print()

print('Max a = {}'.format(arra1))
print('Row and Column of {} is {}'.format(arra1,arra1loc))

我当前的输出:

*** Contents of array a ***
[[459 472 918  50]
 [562 556 145 843]
 [638 797 872  61]]

Max a = 918
Row and Column of 918 is (array([0]), array([2]))

我想要的是什么

*** Contents of array a ***
[[459 472 918  50]
 [562 556 145 843]
 [638 797 872  61]]

Max a = 918
Row and Column of 918 is ([0], [2])

1 个答案:

答案 0 :(得分:0)

您的array1loc实际上是tuple的{​​{1}},正如我们在此处ndarray所见,您可以通过

进行确认
(array([0]), array([2]))

因此,您需要对print(type(arra1loc)) # <class 'tuple'> print(type(arra1loc[0])) # <class 'numpy.ndarray'> 中的每个ndarray应用tolist()

tuple
相关问题