Home > Software engineering >  generate set number of names used by shell split naming profile
generate set number of names used by shell split naming profile

Time:05-08

ok so when you run the split function from terminal it generates the names it names all the parts starting with an x followed by an 2 letter alphabetical incremental pair. aa,ab,ac ect.

What I am looking to o is generate a list of these names using python and a number.

n=3
#['xaa', 'xab', 'xac']

n = 5
#['xaa', 'xab', 'xac', 'xad', 'xae']

So far I am able to generate enough base numbers off of a number but need to find a way to change the second letter I append for every instance of 'z'

import string

n=30
myNum = round((n/26)   (0.5))
list1 = list(string.ascii_lowercase)*myNum

list1 = list1[:n]

list2sring=""
for i in list1:
 list2sring=list2sring i " "

newstring = "a"   list2sring.strip().replace(" "," a")
print("x"   newstring.replace(" "," x"))

#xaa xab xac xad xae xaf xag xah xai xaj xak xal xam xan xao xap xaq xar xas xat xau xav xaw xax xay xaz xaa xab xac xad

Desired Output:

xaa xab xac xad xae xaf xag xah xai xaj xak xal xam xan xao xap xaq xar xas xat xau xav xaw xax xay xaz xba xbb xbc xbd

CodePudding user response:

You can use string.ascii_lowercase, itertools.product, and string formatting to do what you want.

from string import ascii_lowercase
from itertools import product

def make_patterns(n):
    letters_iter = product(ascii_lowercase, ascii_lowercase)
    return [
        f'x{a}{b}'
        for _, (a, b) in zip(range(n), letters_iter)
    ]

make_patterns(5)
# returns:
['xaa', 'xab', 'xac', 'xad', 'xae']

If you want it as one string for printing:

print(' '.join(make_patterns(5)))
# prints:
xaa xab xac xad xae

CodePudding user response:

You can split list2sring into list of chars and append it to string manually in the for loop

import string

n=30
myNum = round((n/26)   (0.5))
list1 = list(string.ascii_lowercase)*myNum
print(string.ascii_lowercase)

list1 = list1[:n]

list2sring=""
for i in list1:
 list2sring=list2sring i " "

chars = list2sring.strip().split(" ")
newstring = ""
i = 0
for c in chars:
    newstring  = " x"   string.ascii_lowercase[i]   c
    if c == "z":
        i = (i   1) % 26

print(newstring.strip())

  • Related