为了账号安全,请及时绑定邮箱和手机立即绑定

Python 3.7 - 打印 NumPy 数组列表会在实际数组前面打印“array”。为什么?

Python 3.7 - 打印 NumPy 数组列表会在实际数组前面打印“array”。为什么?

ibeautiful 2023-08-15 16:28:57
我是编程新手,几乎不掌握术语,所以请原谅我可能遇到的任何基本问题。我正在尝试列出数组的数组。我在二维数组上编写了一个函数,该函数列出了数组中的最高值及其出现的点。这些点(最大值)形成数组[i,j],为了便于显示,我想将其收集到单个 numpy array 或 list 中max_p。据我所知,最明智的方法是使用numpy.append( max_p, [i,j] ). 问题在于它合并 [i,j])到max_p数组中,以便我得到单个值的数组而不是有序对。所以我决定将整个事情转换成一个列表。在大多数情况下,这很有效——我得到了可以在一行中打印出来的有序对列表。但是,大列表中的数组max_p不会打印为,比如说[a,b]。它们被打印为array([a,b]). 无论我是否使用max_p.tolist()或 ,都会发生这种情况list(max_p)。当然,如果没有实际的代码,这些都没有意义,所以它是:def maxfinder_2D(array):    maxima_array = numpy.array([]) # placeholder array    for i in range(0, 422): # side note: learn how to get dim.s of                             # a multidimensional array        x_array = array [i,:] # set array to be the i-th row       # row_max = numpy.append(row_maxfinder_1D(x_array))        maxima_array = numpy.append(maxima_array, numpy.array([maxfinder_1D(x_array)]))            # We construct a new array, maxima_array, to list the             # maximum of every row in the plot.            # The maximum, then, must be the maximum of this array.    max_2D = maxfinder_1D(maxima_array)    print("The maximum value in the image is: ", max_2D)    global max_p    max_p = []    # This gives us the maximum value. Finding its location is another    # step, though I do wish I could come up with a way of finding the    # location and the maximum in a single step.这是输出(部分):The maximum value in the image is:  255.0 The function attains its maximum of  255.0  on  [array([200., 191.]), array([200., 192.]), array([201., 190.]), array([201., 193.]), array([202., 190.]), array([202., 193.]), array([203., 193.]), array([204., 191.]),...我希望数组显示为简单的,例如[200. , 191.].为什么会出现这种“神器”呢?它与 numpy 如何将数组与列表相关联吗?
查看完整描述

2 回答

?
素胚勾勒不出你

TA贡献1827条经验 获得超9个赞

Python对象有repr和str显示方法


In [1]: x = np.arange(3)

In [2]: x

Out[2]: array([0, 1, 2])

In [3]: repr(x)

Out[3]: 'array([0, 1, 2])'

In [4]: str(x)

Out[4]: '[0 1 2]'

In [5]: print(x)

[0 1 2]

对于列表,它们[,]对于列表本身和repr元素是相同的:


In [6]: alist = [x]

In [7]: alist

Out[7]: [array([0, 1, 2])]

In [8]: repr(alist)

Out[8]: '[array([0, 1, 2])]'

In [9]: str(alist)

Out[9]: '[array([0, 1, 2])]'


查看完整回答
反对 回复 2023-08-15
?
白猪掌柜的

TA贡献1893条经验 获得超10个赞

也许您可以尝试使用数组的字符串表示形式

print("The function attains its maximum of ",maxfinder_2D(img)," on ", np.array2string(max_p))


查看完整回答
反对 回复 2023-08-15
  • 2 回答
  • 0 关注
  • 87 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信