python - 随机播放python数组中的某些项目
问题描述
我需要根据第二个数组来打乱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]
推荐阅读
- spring-integration - Spring Integration 流程调用 REST 服务
- sql - 基于条件创建多个列的 SQL 案例语句(或其他内容)
- matlab - ode45 的初始条件?
- python - Heroku 上的 SMTP 服务器
- javascript - 突出显示 Javascript 的问题:变化不同
- node.js - laravel-elixir 未安装在 Windows 10 中
- excel - 工作表对象声明
- css - VB.Net 查找隐藏在 DOM 中时要单击的类 ID
- python-3.x - 在输出视频流上计算 fps 时遇到问题
- c# - C# / 日期时间 /