在python中生成一个固定长度的随机字符串列表

Ama*_*man 2 python random list python-3.x

我需要生成一个列表,其中包含固定长度的随机字母数字字符串.它将是这样的:list = ['ag5','5b9','c85']我可以使用随机数字制作列表,但我无法制作出既有数字又有字母的字符串.列表将具有固定长度(例如100项).我正在使用python 3.

Eug*_*ash 5

string.digits并且string.ascii_lowercase可以为您提供一组字母数字字符.然后你可以random.choice在列表推导中使用来生成列表:

from random import choice
from string import digits, ascii_lowercase

chars = digits + ascii_lowercase
L = ["".join([choice(chars) for i in range(2)]) for j in range(100)]
print(L)
Run Code Online (Sandbox Code Playgroud)