简体   繁体   中英

Python string generator, in a specific order

I'm making a program, that when run will make a random string from a list of characters that i have defined. The code is:

from random import randint
from random import *
import random
Upperletters=["A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"]
Lowerletters = ["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"]
Symbols = ["!","$","%","^","&","*","(",")","_","-","+","="]
Numbers = ["1","2","3","4","5","6","7","8","9","0"]
random.shuffle(Symbols)
random.shuffle(Numbers)
random.shuffle(Upperletters)
random.shuffle(Lowerletters)
randomlength = randint(8, 12)
stringgenerator = ''.join(choice(Upperletters + Symbols + Numbers + 
Lowerletters) for x in range(randomlength))
print(stringgenerator)
stringgeneratorlower = stringgenerator.lower()
letter_combos = ["qwe","wer","ert","rty","tyu","yui","uio","iop","asd","sdf","dfg","fgh","ghj","hjk","jkl","zxc","xcv","cvb","vbn","bnm"]
keyboard =int(sum(stringgeneratorlower.count(fragment) for fragment in letter_combos))
if keyboard >= 1:
    print("Bad try again")

But my problem is that i would like the string to use the symbols in the order they're added in, so it should be uppercase characters first, then some symbols then some numbers and then the lowercase. My problem is, it generates them in a completely random order. How can i make it generate them in the order i would like, so that the string generated doesn't contain any of the letter_combos from the list?

One way to do this is to assign a weight to each category, and then sort the generated string by those weights:

import string
from random import choice, randint

symbols = '!$%^&*()_-+='

categories = [
    string.ascii_uppercase,
    symbols,
    string.digits,
    string.ascii_lowercase,
]
choices = ''.join(categories)
weights = {char: n for n, chars in enumerate(categories) for char in chars}

length = randint(8, 12)
rand_unordered = [choice(choices) for _ in range(length)]
rand_ordered = ''.join(sorted(rand_unordered, key=weights.get))

EDIT FIXED IT I only started coding a year ago and have never used sorted so I didn't bother with it. I changed up your code a bit. I removed Shuffle because why should you randomly pick something from a shuffled list? that's the same as randomly picking from a sorted one! Full code

def shuffle():
    randomlength = randint(8, 12)
    pre = {0: '', 1:'',2:'',3:''}
    lists = {0: Upperletters, 1: Lowerletters, 2: Symbols, 3: Numbers}
    for x in range(4): pre[x]+=(choice(lists[x]))
    for i in range(randomlength-4):
        x = randint(1, 74)
        if x < 27: pre[0]+=choice(lists[0])
        elif x < 53: pre[1]+=choice(lists[1])
        elif x < 65: pre[2]+=choice(lists[2])
        else: pre[3]+=choice(lists[3])
    pw = ''
    for v in pre.values():
           pw += v
    return pw
print(shuffle())

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM