[英]Generating random strings each time
I want to generate Serials using python but I have run into an issue. 我想使用python生成序列号,但是遇到了问题。
I have two different ways of generating serials which I want to use simultaneously. 我有两种不同的生成序列的方式,这些序列想同时使用。 (example below).
(下面的示例)。
charList = ["A", "B", "C", "D", "C", "E", "F", "G", "H", "I", "J", "K", "L", "M",
"O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "1", "2", "3", "4", "5"
"6", "7", "8", "9"]
serial1 = "200" + random.choice(charList)
serial2 = "300" + random.choice(charList)
for i in range (20):
print(serial1)
I want the print to output serial1 and serial2 mixed so you'd maybe get something like this: 我希望打印输出的是serial1和serial2混合的,所以您可能会得到以下信息:
200A
200B
300K
300N
300Z
300C
2001
300K
You could try the choice. 您可以尝试选择。
import random
charList = ["A", "B", "C", "D", "C", "E", "F", "G", "H", "I", "J", "K", "L", "M",
"O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "1", "2", "3", "4", "5"
"6", "7", "8", "9"]
def serial(charList):
return random.choice(["200", "300"]) + random.choice(charList)
for i in range (20):
print(serial(charList))
This will generate your list of serials. 这将生成您的序列号列表。
Something like this? 像这样吗
import random
import string
def get_random_pattern():
return random.choice(["200", "300"])
for _ in range(20):
print(get_random_pattern() + random.choice(string.ascii_uppercase + string.digits))
This should be your desired outcome. 这应该是您想要的结果。
The string module is extremely convenient for getting a iterable with all letters / digits :) 字符串模块非常方便获得所有字母/数字的迭代:)
Next to that, your code violates some PEP8 rules, for example your naming convention. 紧接着,您的代码违反了一些PEP8规则,例如您的命名约定。 In python we prefer to use snake_case, over camelCase.
在python中,相对于camelCase,我们更喜欢使用snake_case。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.