Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
569 views
in Technique[技术] by (71.8m points)

python - 具有大写字母和数字的随机字符串生成(Random string generation with upper case letters and digits)

I want to generate a string of size N.

(我想生成一个大小为N的字符串。)

It should be made up of numbers and uppercase English letters such as:

(它应该由数字和大写英文字母组成,例如:)

  • 6U1S75

    (6U1S75)

  • 4Z4UKK

    (4Z4UKK)

  • U911K4

    (U911K4)

How can I achieve this in a pythonic way?

(我如何以pythonic方式实现这一目标?)

  ask by Hellnar translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Answer in one line:

(一行回答:)

''.join(random.choice(string.ascii_uppercase + string.digits) for _ in range(N))

or even shorter starting with Python 3.6 using random.choices() :

(甚至更短,使用random.choices()从Python 3.6开始:)

''.join(random.choices(string.ascii_uppercase + string.digits, k=N))

A cryptographically more secure version;

(加密更安全的版本;)

see https://stackoverflow.com/a/23728630/2213647 :

(参见https://stackoverflow.com/a/23728630/2213647)

''.join(random.SystemRandom().choice(string.ascii_uppercase + string.digits) for _ in range(N))

In details, with a clean function for further reuse:

(详细而言,具有清除函数以进一步重用:)

>>> 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'

How does it work ?

(它是如何工作的 ?)

We import string , a module that contains sequences of common ASCII characters, and random , a module that deals with random generation.

(我们导入string (一个包含常见ASCII字符序列的模块)和random (一个处理随机生成的模块)。)

string.ascii_uppercase + string.digits just concatenates the list of characters representing uppercase ASCII chars and digits:

(string.ascii_uppercase + string.digits仅连接表示大写ASCII字符和数字的字符列表:)

>>> string.ascii_uppercase
'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
>>> string.digits
'0123456789'
>>> string.ascii_uppercase + string.digits
'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'

Then we use a list comprehension to create a list of 'n' elements:

(然后,我们使用列表推导创建“ n”个元素的列表:)

>>> 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']

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 ).

(在上面的例子中,我们使用[创建列表,但我们不要在id_generator功能,所以Python没有在内存中创建的列表中,但在运行中产生的元素,一个接一个(更多相关信息点击这里 ) 。)

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:

(与其要求创建字符串elem 'n'倍,我们不要求Python创建一个从字符序列中选取的随机字符的'n'倍:)

>>> random.choice("abcde")
'a'
>>> random.choice("abcde")
'd'
>>> random.choice("abcde")
'b'

Therefore random.choice(chars) for _ in range(size) really is creating a sequence of size characters.

(因此random.choice(chars) for _ in range(size)实际上正在创建一个size字符序列。)

Characters that are randomly picked from chars :

(从chars中随机挑选的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'

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...