简体   繁体   中英

Why is my variable 'D8' getting updated in in the second line?

D8 = D7

D7[np.eye(D7.shape[0], D7.shape[1]).astype('bool')] = np.nan

I want to save D7 as D8 before the second line, but running the second line somehow changes D8???

I'm lost as to why this is happening.

I expected D8 to remain the same after the second line, but I am getting NaN along the diagonals in both D7 and D8 when I only want it in D7.

D8 is a reference to D7, not a copy. If you want a true copy of D7, use D8 = D7.copy().

– MattDMo 7 mins ago

Thank you MattDMo!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM