使用 Python 如何生成 200 个激活码(或者优惠券)
import string, random
#激活码中的字符和数字
field = string.letters + string.digits
#获得四个字母和数字的随机组合
def getRandom():
return "".join(random.sample(field,4))
# 生成的每个激活码中有几组
def concatenate(group):
return "-".join([getRandom()
for i in range(group)])
#生成n组激活码
def generate(n):
return [concatenate(4)
for i in range(n)]
if __name__ == '__main__':
print generate(200)
获得结果;
['RVpu-mOvh-DeG6-BRNc', 'A9kL-HMxY-ML7d-av2n', 'FAsK-k34Q-zHiD-u0Hj', 'Htjr-afSz-5zUr-jTgb', 'Z3V4-fP6F-UVAZ-TK9B', '5Mul-pMda-Qycu-eyLY', 'y0CB-Nvsy-l7tv-SwQo', '8Ukv-kKvW-7OvW-Hfqz', 'gbt0-LJ6D-GJPA-Fzrb', 'D70R-fDL9-yt73-4KIc']
[Finished in 0.1s]