Shuffle the sequence x in place. The
optional argument random is a 0-argument function returning a random
float in [0.0, 1.0); by default, this is the function random().
random.shuffle(words)
print(words)
#loop
for word in words:
print(word)
将提供:
IN : words = ['a', 'b', 'c']
OUT : ['c', 'a', 'b'] #This is random
正如@John指出的,只要使用
random.shuffle
。你知道吗将提供:
注1:使用
shuffle
的优点是没有两次获得相同元素的风险。你知道吗注意2:但是要小心,因为使用shuffle也会影响存储数据的原始变量。如果您想保留原始序列,我建议您将数据复制到另一个变量中,并在
shuffle
中使用它。你知道吗相关问题 更多 >
编程相关推荐