生成指定数量的随机数,且不重复

 

多个字符中选取特定数量的字符:
>>> import random
random.sample('abcdefghij',3)
['a', 'd', 'b']
多个字符中选取特定数量的字符组成新字符串:
>>> import random
>>> import string
>>> string.join(random.sample(['a','b','c','d','e','f','g','h','i','j'], 3)).r
eplace(" ","")
'fih'
随机选取字符串:
>>> import random
>>> random.choice ( ['apple', 'pear', 'peach', 'orange', 'lemon'] )
'lemon'
洗牌:
>>> import random
>>> items = [1, 2, 3, 4, 5, 6]
>>> random.shuffle(items)
>>> items
[3, 2, 5, 6, 4, 1]

 

参考:
https://www.cnblogs.com/mfryf/p/4556007.html
https://blog.csdn.net/mingzznet/article/details/39060069

posted @ 2019-09-09 10:24  anobscureretreat  阅读(909)  评论(0编辑  收藏  举报