我想要一些可以拆分一维数组的东西:
np.array([600, 400, 300, 600, 100, 0, 2160])
基于一个值转换成一个二维数组,例如500 这样结果数组应该看起来像
500 | 100 | 0 | 0 | 0
400 | 0 | 0 | 0 | 0
300 | 0 | 0 | 0 | 0
500 | 100 | 0 | 0 | 0
100 | 0 | 0 | 0 | 0
0 | 0 | 0 | 0 | 0
500 | 500 | 500 | 500 | 160
我们从左边填写可能有多少个 500,最后一个作为提醒。
我正在考虑使用 np.divmod() 但不知道如何构造数组本身。
答案 0 :(得分:5)
这是一个最小/最大问题,而不是除法。
import numpy as np
arr = np.array([600, 400, 300, 600, 100, 0, 2160 ])
res = np.zeros( (7, 6), dtype = np.int64)
res[:] = arr[:,None]
res -= np.arange( 0, 3000, 500 ) # Subtract successive 500s from arr.
res = np.clip( res, 0, 500 ) # Clip results to lie >= 0 and <= 500
res
# array([[500, 100, 0, 0, 0, 0],
# [400, 0, 0, 0, 0, 0],
# [300, 0, 0, 0, 0, 0],
# [500, 100, 0, 0, 0, 0],
# [100, 0, 0, 0, 0, 0],
# [ 0, 0, 0, 0, 0, 0],
# [500, 500, 500, 500, 160, 0]])
或作为单衬
np.clip( arr[:,None] - np.arange(0,3000,500), 0, 500 )
在更一般的函数下面遵循疯狂物理学家的评论
def steps_of( arr, step ):
temp = arr[:, None] - np.arange( 0, step * ( arr.max() // step + 1), step)
return np.clip( temp, 0, step )