Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/352.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Numpy排序数组的循环错误,但原始工作正常_Python_Arrays_Sorting_Numpy_For Loop - Fatal编程技术网

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

Python Numpy排序数组的循环错误,但原始工作正常,python,arrays,sorting,numpy,for-loop,Python,Arrays,Sorting,Numpy,For Loop,在对这个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.

在对这个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()
以下是输出:

line 42, in main
for x,y in np.nditer(done,flags = ['external_loop'], order = 'F'):
ValueError: too many values to unpack (expected 2)
this is d reshape of original/start array: [2 5 1 4 2 8 3 3 4 7 7 8]
this is e reshape of d:
 [[2 5]
 [1 4]
 [2 8]
 [3 3]
 [4 7]
 [7 8]]
this is f unique of e:
 [[3 3]
 [1 4]
 [2 5]
 [4 7]
 [2 8]]
this is almost done:
 [3 3 1 4 2 5 4 7 2 8]
this is done:
 [[3 1 2 4 2]
 [3 4 5 7 8]]
this is original array:
 [[2 1 2 3 4 7]
 [5 4 8 3 7 8]]
为什么for循环适用于原始数组,但不适用于此排序数组? 或者我可以使用什么循环来使用(f),因为它是排序的,但形状是(-1,2)

我还尝试了一个不同的循环:

for x,y in done[np.nditer(done,flags = ['external_loop'], order = 'F')]:
这似乎修复了过多值错误,但我得到:

IndexError: index 3 is out of bounds for axis 0 with size 2

我已经在stackexchange上查找到了要修复的错误,但无论我如何执行语法,都会不断得到错误


任何帮助都很好,谢谢

我没有
图形
软件包(它可能是windows特有的东西?),但我知道你把这个问题弄得太复杂了。下面是一个更简单的版本,它生成相同的
done
数组:

from graphics import *
import numpy as np

# starting array
startArray = 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
将其更改回

以下是关于原始代码的一些注释,以供将来参考:

  • 重塑
    中的
    顺序
    标志对于您的代码尝试执行的操作来说是完全多余的,它只会使代码更加混乱/可能更容易出错。您可以在没有它的情况下完成所有想要的重塑

  • nditer
    的用例是一次迭代一个(或多个)数组的单个元素。它通常不能用于迭代二维数组的行或列。如果您尝试以这种方式使用它,可能会得到错误的结果,这些结果高度依赖于内存中阵列的布局(如您所见)

  • 要迭代二维数组的行或列,只需使用简单迭代。如果您只是在一个数组上迭代(例如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]])

您是否尝试运行此处列出的实际程序?win是在main()中本地定义的,因此不适用于win.getMouse()和win.close()(甚至在它们之后调用main()),因此它会导致第46行出现“NameError:名称'win'未定义”。与您自己的程序相比,您是否删除了上述列表中的某些内容,而忘记运行实际列出的程序?确保您运行列出的整个程序,并且它确实给出了您提到的错误。只是猜测一下,似乎
done
array是一个视图(几乎完成了
almostdone
),而
nditer
无法对其进行操作。如果使用
done.copy()
,它似乎可以工作。我认为nditer是用于迭代数组中的单个元素,而不是整个列或其他。“external_loop”(外部循环)选项用于将最终循环交给向量化函数。下面的tel答案将其简化为一组,并且有效。作为对Jesper的回应,该程序将运行它。这只是for循环给了我错误,我可能在发布它的时候把win()放在了顶部,这是我第一次问问题。我通常会在stackexchange的某个地方找到答案,所以我从来没有发布过我自己的问题。@Troy,为什么特定的重塑是一个视图(几乎完成),这是否意味着所有的重塑都是他们正在重塑的视图?哇!!我觉得自己很愚蠢。这要简单得多。每次我尝试使用unique而不进行所有这些重塑时,它都会在排序过程中扰乱x、y值。所以它会对它们进行排序,但是剩余的x值没有链接到它们正确的y。我需要进一步研究T.的功能。@ScoobyDoo
arr.T
相当于
np.transpose(arr)
。对于2D数组,您可以将其视为数组的倾斜:行变为列,列变为行。我明白了,这显然比我使用的更有效,这正是我的问题所需要的。非常感谢。
arr.T==np.array([[0, 4],
                 [1, 5],
                 [2, 6],
                 [3, 7]])