Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Continous alphabetic list in python and getting every value of it

Tags:

I've almost the same problem like this one: How to make a continuous alphabetic list python (from a-z then from aa, ab, ac etc)

But, I am doing a list in gui like excel, where on the vertical header should be letters ...aa,ab,ac....dg,dh,di... To do it, I have to declare every place on my list to certain letter. It is probably impossible with yield.

I mean, let me say, I have 100 of cells and I want to name them all differently. Cell 1 should be "A", Cell 2 should be "B".... Cell 27 should be "AA" and so one. You know it probably from excel. I could do it manually, but it is going to take a lot of time.

Well, I tried to play a little with this code underneath, but without success. I know that there should be a loop somewhere, but I have no idea where.

from string import ascii_lowercase
import itertools

def iter_all_strings():
    for size in itertools.count(1):
        for s in itertools.product(ascii_lowercase, repeat=size):
            yield "".join(s)

for s in iter_all_strings():
    print(s)
    if s == 'bb':
        break

The scope: "for s in iter_all_strings():" is counting until the break. I would say here should be my loop for iteration for my cells. There's just no place for that.

like image 824
MKey69 Avatar asked Jun 04 '19 23:06

MKey69


People also ask

How do I get a list of all letters in Python?

The easiest way to load a list of all the letters of the alphabet is to use the string. ascii_letters , string. ascii_lowercase , and string. ascii_uppercase instances.


1 Answers

Another alternative, if you want to dive deeper (create up to ~18,000 columns):

from string import ascii_lowercase

letters = list(ascii_lowercase)
num_cols = 100

excel_cols = []
for i in range(0, num_cols - 1):
    n = i//26
    m = n//26
    i-=n*26
    n-=m*26
    col = letters[m-1]+letters[n-1]+letters[i] if m>0 else letters[n-1]+letters[i] if n>0 else letters[i]
    excel_cols.append(col)
like image 66
Yaakov Bressler Avatar answered Nov 15 '22 05:11

Yaakov Bressler