在Python中随机化列表 [英] Randomizing a list in Python
本文介绍了在Python中随机化列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想知道是否有一种摇动" Python中的项目列表的好方法.例如,[1,2,3,4,5]
可能会被摇动/随机化为[3,1,4,2,5]
(可能有任何顺序).
I am wondering if there is a good way to "shake up" a list of items in Python. For example [1,2,3,4,5]
might get shaken up / randomized to [3,1,4,2,5]
(any ordering equally likely).
推荐答案
from random import shuffle
list1 = [1,2,3,4,5]
shuffle(list1)
print list1
---> [3, 1, 2, 4, 5]
这篇关于在Python中随机化列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文