如何以pythonic方式选择随机字符? [英] How can I select random characters in a pythonic way?
本文介绍了如何以pythonic方式选择随机字符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想在python中生成一个10个字母数字字符的长字符串.因此,这是从字母数字字符列表中选择随机索引的一部分.
I want to generate a 10 alphanumeric character long string in python . So here is one part of selecting random index from a list of alphanumeric chars.
我的计划:
set_list = ['a','b','c' ........] # all the way till I finish [a-zA-Z0-9]
index = random() # will use python's random generator
some_char = setlist[index]
有没有更好的随机选择角色的方法?
Is there a better way of choosing a character randomly ?
推荐答案
通常的方法是random.choice()
The usual way is random.choice()
>>> import string
>>> import random
>>> random.choice(string.ascii_letters + string.digits)
'v'
这篇关于如何以pythonic方式选择随机字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文