猿问

用于循环错误的 Numpy 排序数组,但原始工作正常

在对这个 numpy 数组进行排序并删除所有重复 (y) 值和重复 (y) 值的相应 (x) 值后,我使用 for 循环在剩余坐标处绘制矩形。但我收到错误:ValueError:解包的值太多(预期为 2),但它的形状与原始形状相同,只是重复项已被删除。


from graphics import *

import numpy as np


def main():

    win = GraphWin("A Window", 500, 500)


# starting array

startArray = np.array([[2, 1, 2, 3, 4, 7],

              [5, 4, 8, 3, 7, 8]])


# the following reshapes the from all x's in one row and y's in second row

# to x,y rows pairing the x with corresponding y value.

# then it searches for duplicate (y) values and removes both the duplicate (y) and

# its corresponding (x) value by removing the row.

# then the unique [x,y]'s array is reshaped back to a [[x,....],[y,....]] array to be used to draw rectangles.

d = startArray.reshape((-1), order='F')


# reshape to [x,y] matching the proper x&y's together

e = d.reshape((-1, 2), order='C')


# searching for duplicate (y) values and removing that row so the corresponding (x) is removed too.

f = e[np.unique(e[:, 1], return_index=True)[1]]


# converting unique array back to original shape

almostdone = f.reshape((-1), order='C')


# final reshape to return to original starting shape but is only unique values

done = almostdone.reshape((2, -1), order='F')


# print all the shapes and elements

print("this is d reshape of original/start array:", d)

print("this is e reshape of d:\n", e)

print("this is f unique of e:\n", f)

print("this is almost done:\n", almostdone)

print("this is done:\n", done)

print("this is original array:\n",startArray)


# loop to draw a rectangle with each x,y value being pulled from the x and y rows

# says too many values to unpack?

for x,y in np.nditer(done,flags = ['external_loop'], order = 'F'):

    print("this is x,y:", x,y)

    print("this is y:", y)

    rect = Rectangle(Point(x,y),Point(x+4,y+4))

    rect.draw(win)


win.getMouse()

win.close()


main()



为什么 for 循环适用于原始数组而不适用于这个排序数组?或者我可以使用什么循环来使用(f),因为它已排序但形状(-1,2)?



牧羊人nacy
浏览 127回答 1
1回答

素胚勾勒不出你

我没有这个graphics包(它可能是特定于 Windows 的东西?),但我知道你让这个 waaaaay 太复杂了。这是一个更简单的版本,它生成相同的done数组:from graphics import *import numpy as np# starting arraystartArray = np.array([[2, 1, 2, 3, 4, 7],                       [5, 4, 8, 3, 7, 8]])# searching for duplicate (y) values and removing that row so the corresponding (x) is removed too.done = startArray.T[np.unique(startArray[1,:], return_index=True)[1]]for x,y in done:    print("this is x,y:", x, y)    print("this is y:", y)    rect = Rectangle(Point(x,y),Point(x+4,y+4))    rect.draw(win)请注意,在上面的版本done.shape==(5, 2)中(2, 5),而不是,但您始终可以在for循环后使用 将其更改回来done = done.T。以下是有关原始代码的一些注释,以供将来参考:该order旗reshape完全是多余的,以你的代码试图做的,只是使它更加混乱/可能更马车。没有它,你可以做所有你想做的重塑。for 的用例nditer是一次迭代一个(或多个)数组的各个元素。它通常不能用于迭代二维数组的行或列。如果您尝试以这种方式使用它,您可能会得到高度依赖于内存中数组布局的错误结果(如您所见)。要迭代二维数组的行或列,只需使用简单的迭代。如果你只是遍历一个数组(例如for row in arr:),你会得到每一行,一次一个。如果你想要列,你可以先转置数组(就像我在上面的代码中所做的那样.T)。注意事项 .T.T接受数组的转置。例如,如果您从arr = np.array([[0, 1, 2, 3],                [4, 5, 6, 7]])那么转置是:arr.T==np.array([[0, 4],                 [1, 5],                 [2, 6],                 [3, 7]])
随时随地看视频慕课网APP

相关分类

Python
我要回答