我試圖在第四個角色之后添加 1、2、3、4、5,這意味著我想從 10x10 陣列的第 5 行開始添加數字。下面附上我的代碼。
big_arr = np.zeros((10,10),dtype=np.uint8)
count = 0
for x in range(0,10):
for y in range(0,10):
if big_arr[x,y] > 50:
big_arr[x,y] = count 1
count = 1
我不確定為什么 big_arr[x,y] 的值為 0,如果我將運算子切換為 < 它將連續加 1。行和列達到50后,值不是會增加嗎?謝謝
uj5u.com熱心網友回復:
如果您正在使用 numpy 并執行 for 回圈,那么您很可能做錯了什么。
numpy 通過切片實作的速度。
import numpy as np
my_array=np.zeros((10,10))
print(my_array)
#[[0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
# [0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
# [0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
# [0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
# [0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
# [0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
# [0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
# [0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
# [0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
# [0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]]
fill_array = np.arange(1,51).reshape(5,10)
print(fill_array)
#[[ 1 2 3 4 5 6 7 8 9 10]
# [11 12 13 14 15 16 17 18 19 20]
# [21 22 23 24 25 26 27 28 29 30]
# [31 32 33 34 35 36 37 38 39 40]
# [41 42 43 44 45 46 47 48 49 50]]
my_array[5:,:] = fill_array
print(my_array)
#[[ 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
# [ 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
# [ 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
# [ 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
# [ 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
# [ 1. 2. 3. 4. 5. 6. 7. 8. 9. 10.]
# [11. 12. 13. 14. 15. 16. 17. 18. 19. 20.]
# [21. 22. 23. 24. 25. 26. 27. 28. 29. 30.]
# [31. 32. 33. 34. 35. 36. 37. 38. 39. 40.]
# [41. 42. 43. 44. 45. 46. 47. 48. 49. 50.]]
uj5u.com熱心網友回復:
您好,我剛剛找到了答案。
big_arr = np.zeros((10,10),dtype=np.uint8)
count = 0
for x in range(5,10):
for y in range(0,10):
big_arr[x,y] = count 1
count = 1
我所做的只是將 x 的范圍更改為 5,然后值開始生效。謝謝。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/510378.html
上一篇:如何使用輸入函式匹配亂數
