很简单:
- 将您的数字除以 1, 10, 100, 1000, ... 向下舍入
- 将结果除以 10
这产生
l // 10 ** np.arange(10)[:, None] % 10
或者如果您想要一个适用于的解决方案
- any base
- 任意数量的数字和
- 任意数量的维度
你可以做
l = np.random.randint(0, 1000000, size=(3, 3, 3, 3))
l.shape
# (3, 3, 3, 3)
b = 10 # Base, in our case 10, for 1, 10, 100, 1000, ...
n = np.ceil(np.max(np.log(l) / np.log(b))).astype(int) # Number of digits
d = np.arange(n) # Divisor base b, b ** 2, b ** 3, ...
d.shape = d.shape + (1,) * (l.ndim) # Add dimensions to divisor for broadcasting
out = l // b ** d % b
out.shape
# (6, 3, 3, 3, 3)