【发布时间】:2019-06-27 21:49:32
【问题描述】:
我需要数组包含我正在生成的(x,y) 坐标的所有可能组合。该数组仍然打印为1s 的数组。
import numpy as np
coordinates = np.ones([1000, 2])
def translate (x,y):
dx = 5
dy = 5
return x + dx, y + dy
for i in range(0, 100):
for j in range(0, 100):
(x, y) = translate(i, j)
coordinates[i, j] = translate(x, y)
np.append(coordinates, translate(x, y), axis=None)
print(coordinates)
我希望坐标数组在调用 translate 函数后接收正确的值,而不是 1s 的数组。
【问题讨论】:
-
你的代码给了我错误
"ValueError: setting an array element with a sequence."因为translate返回元组但数组需要单个数字。 -
还有其他问题:您创建了大小为 1000x2 的数组,但后来您使用 for 循环,如数组 100x100。这给出了错误
"IndexError: index 2 is out of bounds for axis 1 with size 2" -
你为什么要给
translate()打3次电话? -
我只是在尝试什么可行
-
您没有仔细阅读
np.append文档。这不是列表克隆。