Skip to content Skip to sidebar Skip to footer

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 t

Solution 1:

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 inrange(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>0elseletters[n-1]+letters[i]if n>0elseletters[i]
    excel_cols.append(col)

Solution 2:

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.

fromstring 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 initer_excel_columns():
    print(s)
    if s == 'bb':
        break

This gives the printout that you apparently want:

ab
c
d
e
f
g
h
i
j
k
l
m
n
o
pq
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

Solution 3:

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 stringimport 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:

defget_loops_needed(num_cols):
    loops_needed = 0
    temp = num_cols
    whileTrue:
        temp = temp / len(letters)
        loops_needed += 1if temp == 0:
            breakreturn loops_needed

Post a Comment for "Continous Alphabetic List In Python And Getting Every Value Of It"