我才刚刚开始学习如何使用numpy。我有这段代码,并希望使用numpy数组来表示它。有关如何执行此操作的任何提示?
import numpy as np
for i in range(np):
direction = random.randint(1, 4)
if direction == NORTH:
ypositions[i] += 1
elif direction == SOUTH:
ypositions[i] -= 1
elif direction == EAST:
xpositions[i] += 1
elif direction == WEST:
xpositions[i] -= 1
答案 0 :(得分:1)
使用numpy时,您需要考虑整个数组。因此,基于某个随机数应用方向性增量需要同时对所有坐标进行操作。
例如(假设xposition和yposition是numpy数组):
directions = np.random.randint(1,4,25)
ypositions += directions == NORTH
ypositions -= directions == SOUTH
xpositions += directions == EAST
xpositions -= directions == WEST