I'm new to programming and have practically no grasp on terminology, so please excuse me for any basic questions I might have.
I'm trying to list an array of arrays.
I have written a function over a 2D array that lists the highest value in the array and the points where it occurs. These points (maxima) form arrays [i,j]
which, for ease of display, I want to collect into a single numpy array or list max_p
. To my knowledge, the most sensible way to do that is with numpy.append( max_p, [i,j] )
. The issue with this is that it merges [i,j])
into the max_p
array so that I get an array of single values instead of ordered pairs. So I decided to convert the whole thing into a list.
This worked well, for the most part- I got my list of ordered pairs I can print off in a single line.
However, the arrays in the big list max_p
are not printed as, say [a,b]
. They're printed as array([a,b])
. This happens regardless of whether I use max_p.tolist()
or list(max_p)
.
And, of course, since none of this is meaningful without the actual code, here it is:
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.
for i in range(0,422):
for j in range(400): # the nested loop runs over the entire array
val = img[i][j]
if val == max_2D:
max_pos_array = numpy.array([])
max_pos_array = numpy.append(max_pos_array , i)
max_pos_array = numpy.append(max_pos_array , j)
list(max_pos_array)
#print(max_pos_array.tolist())
max_p.append(max_pos_array)
return max_2D
print("The function attains its maximum of ",maxfinder_2D(img)," on ", max_p)
And here is (part of) the output:
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.]),
...
I want the arrays to show up as simply, for example, [200. , 191.]
.
Why does this "artifact" occur? Does it have something to do with how numpy relates arrays to lists?
EDIT: As it turns out all I needed to do was to treat max_pos_array as a list as well, but I'm still curious about why, exactly, this happens.