这就是我所拥有的:
>>> import random
>>> chars = [1, 6, 7, 8, 5.6, 3]
>>> for k in range(1, len(chars)+1):
... print random.choice(chars)
... chars[random.choice(chars)] = ''
...
但是当我运行它时,
5.6
1
5.6
8
>>>
我不希望它打印每个内容的随机数量,我希望它以随机顺序打印所有内容一次。为什么它打印空格?
最佳答案
试试这个:
import random
chars = [1, 6, 7, 8, 5.6, 3]
r = chars [:] #make a copy in order to leave chars untouched
random.shuffle (r) #shuffles r in place
print (r)
关于python - 如何获取一个列表并以随机顺序打印所有内容,并在 python 中打印每个内容一次?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21583794/