有没有一种更简单的重复序列的方法,使它们每次都有不同的结果?
Is there a simpler way of repeating sequences so they have a different outcome each time?
所以我为密码生成器编写了这段(邋遢的)代码(记住我是初学者):
import random
letter_list = [chr(i) for i in range(ord('a'),ord('z')+1)]
number_list = range(0,9,1)
character_list = ["£","$","%","&","*","!","?","@"]
capital_list = [chr(i) for i in range(ord('A'),ord('Z')+1)]
random_list = [random.choice(letter_list),random.choice(number_list),random.choice(character_list),random.choice(capital_list)]
seqs = letter_list, number_list, character_list, capital_list, random_list
item = random.choice(random.choices(seqs, weights=map(len, seqs))[0])
item1 = random.choice(random.choices(seqs, weights=map(len, seqs))[0])
item2 = random.choice(random.choices(seqs, weights=map(len, seqs))[0])
item3 = random.choice(random.choices(seqs, weights=map(len, seqs))[0])
item4 = random.choice(random.choices(seqs, weights=map(len, seqs))[0])
item5 = random.choice(random.choices(seqs, weights=map(len, seqs))[0])
item6 = random.choice(random.choices(seqs, weights=map(len, seqs))[0])
item7 = random.choice(random.choices(seqs, weights=map(len, seqs))[0])
item8 = random.choice(random.choices(seqs, weights=map(len, seqs))[0])
item9 = random.choice(random.choices(seqs, weights=map(len, seqs))[0])
item10 = random.choice(random.choices(seqs, weights=map(len, seqs))[0])
item11 = random.choice(random.choices(seqs, weights=map(len, seqs))[0])
item12 = random.choice(random.choices(seqs, weights=map(len, seqs))[0])
item13 = random.choice(random.choices(seqs, weights=map(len, seqs))[0])
item14 = random.choice(random.choices(seqs, weights=map(len, seqs))[0])
item15 = random.choice(random.choices(seqs, weights=map(len, seqs))[0])
item16 = random.choice(random.choices(seqs, weights=map(len, seqs))[0])
print(item, item1, item2, item3, item4, item5, item6, item7, item8, item9)
我想知道是否有更简单的方法来写这部分,因为它看起来太长了,不是最简单的选择:
seqs = letter_list, number_list, character_list, capital_list, random_list
item = random.choice(random.choices(seqs, weights=map(len, seqs))[0])
item1 = random.choice(random.choices(seqs, weights=map(len, seqs))[0])
item2 = random.choice(random.choices(seqs, weights=map(len, seqs))[0])
item3 = random.choice(random.choices(seqs, weights=map(len, seqs))[0])
item4 = random.choice(random.choices(seqs, weights=map(len, seqs))[0])
item5 = random.choice(random.choices(seqs, weights=map(len, seqs))[0])
item6 = random.choice(random.choices(seqs, weights=map(len, seqs))[0])
item7 = random.choice(random.choices(seqs, weights=map(len, seqs))[0])
item8 = random.choice(random.choices(seqs, weights=map(len, seqs))[0])
item9 = random.choice(random.choices(seqs, weights=map(len, seqs))[0])
item10 = random.choice(random.choices(seqs, weights=map(len, seqs))[0])
item11 = random.choice(random.choices(seqs, weights=map(len, seqs))[0])
item12 = random.choice(random.choices(seqs, weights=map(len, seqs))[0])
item13 = random.choice(random.choices(seqs, weights=map(len, seqs))[0])
item14 = random.choice(random.choices(seqs, weights=map(len, seqs))[0])
item15 = random.choice(random.choices(seqs, weights=map(len, seqs))[0])
item16 = random.choice(random.choices(seqs, weights=map(len, seqs))[0])
将项目存储在一个列表中,并创建一个字符池以使用列表连接从中提取。
# ... [same as in original code]
# transform to list to allow for concatenation
number_list = list(str(i) for i in range(9))
# ... [same as in original code]
seqs = letter_list + number_list + character_list + capital_list + random_list
items = [random.choice(seqs) for _ in range(18)]
print(' '.join(item for item in items[:10]))
所以我为密码生成器编写了这段(邋遢的)代码(记住我是初学者):
import random
letter_list = [chr(i) for i in range(ord('a'),ord('z')+1)]
number_list = range(0,9,1)
character_list = ["£","$","%","&","*","!","?","@"]
capital_list = [chr(i) for i in range(ord('A'),ord('Z')+1)]
random_list = [random.choice(letter_list),random.choice(number_list),random.choice(character_list),random.choice(capital_list)]
seqs = letter_list, number_list, character_list, capital_list, random_list
item = random.choice(random.choices(seqs, weights=map(len, seqs))[0])
item1 = random.choice(random.choices(seqs, weights=map(len, seqs))[0])
item2 = random.choice(random.choices(seqs, weights=map(len, seqs))[0])
item3 = random.choice(random.choices(seqs, weights=map(len, seqs))[0])
item4 = random.choice(random.choices(seqs, weights=map(len, seqs))[0])
item5 = random.choice(random.choices(seqs, weights=map(len, seqs))[0])
item6 = random.choice(random.choices(seqs, weights=map(len, seqs))[0])
item7 = random.choice(random.choices(seqs, weights=map(len, seqs))[0])
item8 = random.choice(random.choices(seqs, weights=map(len, seqs))[0])
item9 = random.choice(random.choices(seqs, weights=map(len, seqs))[0])
item10 = random.choice(random.choices(seqs, weights=map(len, seqs))[0])
item11 = random.choice(random.choices(seqs, weights=map(len, seqs))[0])
item12 = random.choice(random.choices(seqs, weights=map(len, seqs))[0])
item13 = random.choice(random.choices(seqs, weights=map(len, seqs))[0])
item14 = random.choice(random.choices(seqs, weights=map(len, seqs))[0])
item15 = random.choice(random.choices(seqs, weights=map(len, seqs))[0])
item16 = random.choice(random.choices(seqs, weights=map(len, seqs))[0])
print(item, item1, item2, item3, item4, item5, item6, item7, item8, item9)
我想知道是否有更简单的方法来写这部分,因为它看起来太长了,不是最简单的选择:
seqs = letter_list, number_list, character_list, capital_list, random_list
item = random.choice(random.choices(seqs, weights=map(len, seqs))[0])
item1 = random.choice(random.choices(seqs, weights=map(len, seqs))[0])
item2 = random.choice(random.choices(seqs, weights=map(len, seqs))[0])
item3 = random.choice(random.choices(seqs, weights=map(len, seqs))[0])
item4 = random.choice(random.choices(seqs, weights=map(len, seqs))[0])
item5 = random.choice(random.choices(seqs, weights=map(len, seqs))[0])
item6 = random.choice(random.choices(seqs, weights=map(len, seqs))[0])
item7 = random.choice(random.choices(seqs, weights=map(len, seqs))[0])
item8 = random.choice(random.choices(seqs, weights=map(len, seqs))[0])
item9 = random.choice(random.choices(seqs, weights=map(len, seqs))[0])
item10 = random.choice(random.choices(seqs, weights=map(len, seqs))[0])
item11 = random.choice(random.choices(seqs, weights=map(len, seqs))[0])
item12 = random.choice(random.choices(seqs, weights=map(len, seqs))[0])
item13 = random.choice(random.choices(seqs, weights=map(len, seqs))[0])
item14 = random.choice(random.choices(seqs, weights=map(len, seqs))[0])
item15 = random.choice(random.choices(seqs, weights=map(len, seqs))[0])
item16 = random.choice(random.choices(seqs, weights=map(len, seqs))[0])
将项目存储在一个列表中,并创建一个字符池以使用列表连接从中提取。
# ... [same as in original code]
# transform to list to allow for concatenation
number_list = list(str(i) for i in range(9))
# ... [same as in original code]
seqs = letter_list + number_list + character_list + capital_list + random_list
items = [random.choice(seqs) for _ in range(18)]
print(' '.join(item for item in items[:10]))