具有大写字母和数字的随机字符串生成 [英] Random string generation with upper case letters and digits
问题描述
我想生成一个大小为N的字符串.
I want to generate a string of size N.
它应该由数字和大写英文字母组成,例如:
It should be made up of numbers and uppercase English letters such as:
- 6U1S75
- 4Z4UKK
- U911K4
如何以 pythonic 的方式实现这一目标?
How can I achieve this in a pythonic way?
推荐答案
答案在一行:
''.join(random.choice(string.ascii_uppercase + string.digits) for _ in range(N))
甚至更短,使用 random.choices()
:
or even shorter starting with Python 3.6 using random.choices()
:
''.join(random.choices(string.ascii_uppercase + string.digits, k=N))
加密方式更安全的版本;参见 https://stackoverflow.com/a/23728630/2213647 :
A cryptographically more secure version; see https://stackoverflow.com/a/23728630/2213647:
''.join(random.SystemRandom().choice(string.ascii_uppercase + string.digits) for _ in range(N))
详细信息,具有清除函数以供进一步重用:
>>> import string
>>> import random
>>> def id_generator(size=6, chars=string.ascii_uppercase + string.digits):
... return ''.join(random.choice(chars) for _ in range(size))
...
>>> id_generator()
'G5G74W'
>>> id_generator(3, "6793YUIO")
'Y3U'
它如何工作?
我们导入string
(一个包含常见ASCII字符序列的模块)和random
(一个用于处理随机生成的模块).
We import string
, a module that contains sequences of common ASCII characters, and random
, a module that deals with random generation.
string.ascii_uppercase + string.digits
只是连接表示大写ASCII字符和数字的字符列表:
string.ascii_uppercase + string.digits
just concatenates the list of characters representing uppercase ASCII chars and digits:
>>> string.ascii_uppercase
'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
>>> string.digits
'0123456789'
>>> string.ascii_uppercase + string.digits
'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'
然后,我们使用列表推导来创建"n"个元素的列表:
Then we use a list comprehension to create a list of 'n' elements:
>>> range(4) # range create a list of 'n' numbers
[0, 1, 2, 3]
>>> ['elem' for _ in range(4)] # we use range to create 4 times 'elem'
['elem', 'elem', 'elem', 'elem']
在上面的示例中,我们使用[
创建列表,但是我们不在id_generator
函数中,因此Python不会在内存中创建列表,而是通过以下方式即时生成元素:一个(有关更多信息,请此处).
In the example above, we use [
to create the list, but we don't in the id_generator
function so Python doesn't create the list in memory, but generates the elements on the fly, one by one (more about this here).
我们将要求Python从字符串序列中选择一个随机字符,而不是创建字符串n乘以elem
:
Instead of asking to create 'n' times the string elem
, we will ask Python to create 'n' times a random character, picked from a sequence of characters:
>>> random.choice("abcde")
'a'
>>> random.choice("abcde")
'd'
>>> random.choice("abcde")
'b'
因此,random.choice(chars) for _ in range(size)
实际上是在创建size
字符序列.从chars
:
Therefore random.choice(chars) for _ in range(size)
really is creating a sequence of size
characters. Characters that are randomly picked from chars
:
>>> [random.choice('abcde') for _ in range(3)]
['a', 'b', 'b']
>>> [random.choice('abcde') for _ in range(3)]
['e', 'b', 'e']
>>> [random.choice('abcde') for _ in range(3)]
['d', 'a', 'c']
然后我们将它们与一个空字符串连接起来,以便该序列成为一个字符串:
Then we just join them with an empty string so the sequence becomes a string:
>>> ''.join(['a', 'b', 'b'])
'abb'
>>> [random.choice('abcde') for _ in range(3)]
['d', 'c', 'b']
>>> ''.join(random.choice('abcde') for _ in range(3))
'dac'
这篇关于具有大写字母和数字的随机字符串生成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!