gpt4 book ai didi

python - 没有 for 循环的 Numpy 数组

转载 作者:太空宇宙 更新时间:2023-11-04 04:21:27 25 4
gpt4 key购买 nike

array = np.empty(8,4)

for I in range(8):
array[I] = I

这可以不用for循环实现吗?我想知道其他方法

array = np.empty(8,4)
for I in range(8):
array[I] = I

[0,0,0,0]
[1,1,1,1]
.
.
.
[7,7,7,7]

最佳答案

一种简单的方法是只使用np.repeat:

array = np.repeat(np.arange(8), 4).reshape(8, 4)

array([[0, 0, 0, 0],
[1, 1, 1, 1],
[2, 2, 2, 2],
[3, 3, 3, 3],
[4, 4, 4, 4],
[5, 5, 5, 5],
[6, 6, 6, 6],
[7, 7, 7, 7]])

关于python - 没有 for 循环的 Numpy 数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54426560/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com