我需要根据第二个数组说明应该打乱哪个索引来打乱 python 数组的某些元素。最好就地。
arr = [1,2,3,4,5,6]
indeces_to_shuffle = [0,4,5]
shuffle_algorithm(arr, indeces_to_shuffle) # Need help here!
print(arr)
> 6,2,3,4,1,5
最佳答案
from random import shuffle
arr = [1,2,3,4,5,6]
indeces_to_shuffle = [0,4,5]
vals = [arr[i] for i in indeces_to_shuffle]
shuffle(indeces_to_shuffle)
for i, v in zip(indeces_to_shuffle, vals):
arr[i] = v
print(arr)
打印(例如):
[5, 2, 3, 4, 6, 1]
关于python - 随机播放 python 数组中的某些项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63018611/