Python: shuffle and create a new array -
i want shuffle array, find method random.shuffle(x), best way randomize list of strings in python
can like
import random rectangle = [(0,0),(0,1),(1,1),(1,0)] # want # disorderd_rectangle = rectangle.shuffle now can away with
disorderd_rectangle = rectangle random.shuffle(disorderd_rectangle) print(disorderd_rectangle) print(rectangle) but returns
[(1, 1), (1, 0), (0, 1), (0, 0)] [(1, 1), (1, 0), (0, 1), (0, 0)] so original array changed. how can create shuffled array without changing original one?
people here advising deepcopy, surely overkill. don't mind objects in list being same, want shuffle order. that, list provides shallow copying directly.
rectangle2 = rectangle.copy() random.shuffle(rectangle2) about misconception: please read http://nedbatchelder.com/text/names.html#no_copies
Comments
Post a Comment