So I wrote this (scruffy) code for a password generator (bear in mind I am a beginner):
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)
I was wondering if there was a simpler way to write out this part, as it seems too long to be the simplest option:
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])
CodePudding user response:
Store the items in a list, and create one pool of characters to draw from using list concatenation.
# ... [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]))