0

Get stuck at one point.

I have one list containing n elements so according to length, I have to generate list.

For example i have an list contains 25 element then new list will be [A,B,C,...,Y] and for example list contains 26 elements then [A,B,C,...,Z].

Up to z i can easily get list but now i am getting more than 26 elements. for example length is 27 then i want these type of list [A,B,C,...,Y,Z,AA].

so, how i am able to get these type of list any suggestion ???

Harsh Parekh
  • 103
  • 6

3 Answers3

2

Here is a way to write this with itertools: itr generates an infinite number of combinations, first of length 1, then of length 2, etc. Using islice takes the required number of elements.

from string import ascii_uppercase
from itertools import product, count, islice

itr = ("".join(tup) 
  # choose number of letters, e.g. 1, 2, 3 
  for k in count(1)
  # choose all tuples of k letters, e.g. (A, ), (B, ), ... (A, A,) ...
  for tup in product(ascii_uppercase, repeat=k))

res = list(islice(itr, 28))
hilberts_drinking_problem
  • 11,322
  • 3
  • 22
  • 51
0

you can do something like this:

import string
def letters_list_generator(number):
    alphabet_string = string.ascii_uppercase
    alphabet_list = list(alphabet_string)
    letter_list = []
    for i in range(0,number):
        multiplier = int(i/26)+1
        letter_index = i%26
        string_for_list = alphabet_list[letter_index] * multiplier
        letter_list.append(string_for_list)
    return letter_list
0

For example you can try this code:

list_1 = ['A', 'B', 'C']
list_2 = ['1', '2', '3']
list_3 = []
for i in range(3):
    list_3.append(list_1[i] + list_2[i]

The result is sum = ['A1', 'B2', 'C3']. If you play with the for loop, you can obtain what you want.

Jonny_92
  • 42
  • 9