請參閱我已經撰寫了代碼,這適用于具有異構元素的陣列(例如 [2 3 4 5](不同元素),但是,如果元素是同質的(例如 [2 5 5 8]),則輸出不會無法按預期作業。我該如何解決這個問題?
下面的代碼:
import numpy as np
x = np.array([1, 2, 3, 3, 5])
print("arrray x is" " " str(x))
print()
i = 0
while i<(len(x)):
z = (x[i] 5)
print("The new value for x[" str(i) "] is" " " str(z))
j = 0
while j<(len(x)):
if (x[j] 5) == z:
x = np.where(x==x[j], z, x)
j = 1
i =1
print()
print("Now the array x is" " " str(x))
uj5u.com熱心網友回復:
試試這個。
import numpy as np
x = np.array([1, 2, 3, 3, 5])
new_lst = x 5
print(new_lst)
輸出[6, 7, 8, 8, 10]
謝謝,@matszwecja。
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/487620.html
