user11329096
user11329096

Reputation:

Continous alphabetic list in python and getting every value of it

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.

Upvotes: 5

Views: 1323

Answers (3)

cwalvoort
cwalvoort

Reputation: 1967

Here is another way to approach the problem. This also allows you to give the number of columns you want to generate and will work for any "two character" columns and would also work if you changed the allowed letters for some reason:

from string import ascii_lowercase

letters = list(ascii_lowercase)
num_cols = 100

excel_cols = []
for i in range(0, num_cols - 1):
    col = ""
    if i / len(letters) > 0:
        col = str(letters[i / len(letters) - 1])
    col += letters[i % len(letters)]
    excel_cols.append(col)
print(excel_cols)

#output: ['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', 'aa', 'ab', 'ac', 'ad', 'ae', 'af', 'ag', 'ah', 'ai', 'aj', 'ak', 'al', 'am', 'an', 'ao', 'ap', 'aq', 'ar', 'as', 'at', 'au', 'av', 'aw', 'ax', 'ay', 'az', 'ba', 'bb', 'bc', 'bd', 'be', 'bf', 'bg', 'bh', 'bi', 'bj', 'bk', 'bl', 'bm', 'bn', 'bo', 'bp', 'bq', 'br', 'bs', 'bt', 'bu', 'bv', 'bw', 'bx', 'by', 'bz', 'ca', 'cb', 'cc', 'cd', 'ce', 'cf', 'cg', 'ch', 'ci', 'cj', 'ck', 'cl', 'cm', 'cn', 'co', 'cp', 'cq', 'cr', 'cs', 'ct', 'cu']

If you wanted to work the exercise of going past two character column names, here's a teaser to get you started. The actual implementation is left as an exercise for the reader:

def get_loops_needed(num_cols):
    loops_needed = 0
    temp = num_cols
    while True:
        temp = temp / len(letters)
        loops_needed += 1
        if temp == 0:
            break
    return loops_needed

Upvotes: 1

Yaakov Bressler
Yaakov Bressler

Reputation: 12018

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)

Upvotes: 1

Rory Daulton
Rory Daulton

Reputation: 22544

Try this code. It works by pretending that all Excel column names have two characters, but the first "character" may be the null string. I get the product to accept the null string as a "character" by using a list of characters rather than a string.

from string import ascii_lowercase
import itertools

first_char = [''] + list(ascii_lowercase)
def iter_excel_columns():
    for char1, char2 in itertools.product(first_char, ascii_lowercase):
            yield char1 + char2

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

This gives the printout that you apparently want:

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
aa
ab
ac
ad
ae
af
ag
ah
ai
aj
ak
al
am
an
ao
ap
aq
ar
as
at
au
av
aw
ax
ay
az
ba
bb

Upvotes: 1

Related Questions