简体   繁体   中英

NumPy random.shuffle function

I ran into something strange with numpy.random.shuffle function

from numpy import arange
from numpy.random import shuffle

a = arange(5)
b = a
c = a[:]

shuffle(c)

a and b all changes with c . Actually no matter I shuffle() which variable, the other two all changes with it. I thought when I use slice copy the original variable should be independent. Did I miss something? How can I protect the original variable from being changed?

According to the Basic slicing documentation :

All arrays generated by basic slicing are always views of the original array.

Use ndarray.copy or numpy.copy to get copy.

使用c = a.copy()可以为您提供帮助。

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