Home > Software engineering >  Gerenate a password by ordinal python?
Gerenate a password by ordinal python?

Time:07-30

I want to gerenate some list like this : ["aaa","aab","aac",...] in python. And i want to gerenate one string in one command like this: nextpassword("aac") not gerenate all list in one time. Thanks for help!

CodePudding user response:

You can try in this way. At first, you have to generate a list of password using your conditions. Then, you can choose randomly or sequentially.

import string
import random

# generation of password list using list comprehension
password_list = ['aa' x  for x in string.ascii_lowercase]
# print(password_list)


# choose a random password
print(random.choice(password_list))

CodePudding user response:

def nextpassword(last_item):
    next_char = chr(ord(last_item)   1)
    if next_char in string.ascii_lowercase:
       password_list.append(next_char)
       return next_char
    return False

CodePudding user response:

This should work with any alphabet and offset, I'm using the spectacular more-itertools package.

import more_itertools
def next_string(current, offset=1, alphabet=string.ascii_lowercase):
    alphabet_for_product = [alphabet] * len(current)
    current_index = more_itertools.product_index(current, *alphabet_for_product)
    result = more_itertools.nth_product(current_index   offset, *alphabet_for_product)
    return ''.join(result)

assert next_string('aac') == 'aad'
  • Related