我有一個數組(稱爲圖像),我想要循環並使用numpy.roll函數進行調整。我也想存儲我即將調整的行的副本。這裏是我的代碼:Python,兩個變量應該是不同的,但它們保持不變 - 是np.roll責備?
for i in range(1, 10):
delta = function(i) ### it's not important what this function is, just that it yields an int
x0 = image[i] ### creating a variable to store the row before I change it
print x0[:20] ###only printing the first 20 elements, as it's a very large array
image[i] = np.roll(x0, -delta)
print image[i][:20] ###image[i] has now been updated using np.roll function
if np.array_equal(image[i], x0) == True:
print 'something wrong'
現在是奇怪的事情發生在:當我運行這段代碼,我可以看到,x0和圖像[I]有很大的不同(如每頭20元的打印到屏幕)。不過,我也在屏幕上打印出了「錯誤的東西」,這非常令人困惑,因爲這實現了x0和image [i]是相等的。這是一個問題,因爲我的腳本的其餘部分依賴於x0和image [i]不相等(除非delta = 0),但腳本始終將它們視爲它們。
幫助感謝!
,因爲這個答案不包含任何解釋,並創建這可能幫助你 –
Downvoting一個列表而不是一個數組。 – user2357112
嘿,你不能像列表一樣將列表或數組列表複製到另一個變量中...因爲列表或數組列表被視爲對象,並且當您指定另一個變量時,它們只會成爲對原始對象的引用,並且當您使用引用和詢問其他引用的值,他們給出相同的值 –