I am trying to work out how to achieve an alphabetic increment that is ran over many items as part of a renamer.
I am using chr() and ord() to retrieve the next letter increment after a starting letter such as:
startletter = 'a'
bb = 0
preletters = ''
pinc = 1
for o in objects:
newchar = chr(ord(startletter) + bb)
#rename here such as joining the preletters with the newchar
bb = bb + 1
I then check if the newchar was ‘z’ and then if so make the preletter as follows:
if newchar == 'z':
preletters = chr(pinc)
pinc = pinc + 1
I am a bit lost to make this more efficient, ideally what I want should produce a pattern such as
a,b,c,d ...z for the first 26 items
and then start doubling the letters like this
aa,ab,ac,ad,... az
ba,bb,bc,... bz
once we hit zz, we’d then need to triple them to:
aaa, aab, aac,...
Any help appreciated.
-
What exactly is the problem with your current code?Mureinik– Mureinik2025年04月18日 18:28:27 +00:00Commented Apr 18, 2025 at 18:28
-
It deals with double pairings, but not triple sets eg aaa, aab so its not very dynamicblam– blam2025年04月18日 18:45:16 +00:00Commented Apr 18, 2025 at 18:45
-
Your main problems are the syntax errorsjackal– jackal2025年04月19日 06:52:33 +00:00Commented Apr 19, 2025 at 6:52
4 Answers 4
Not quite sure what you're asking, but far more flexible is get_excel_column_number:
def get_excel_column_name(column_number):
result = []
while column_number > 0:
modulus = (column_number - 1) % 26
result.append(chr(modulus + ord('A')))
column_number = (column_number - modulus) // 26
return ''.join(reversed(result))
This function lets you know that get_excel_column_number(1) is A and the millionth column is BDWGN, without having to look at all the columns in between.
(Note that this function uses 1-indexed columns. Add column_number += 1 as the first line if you want 0-indexed columns.)
And obviously you can create an iterator by:
def my_iterator():
for i in itertools.count(1):
yield get_excel_column_name(i)
I copied the implementation of get_excel_column_name from here and converted it to Python.
(I just realized you wanted lowercase. Just replace 'A' with 'a')
1 Comment
Sure, you can create an infinite iterator (so careful, don't try to materialize all of it, e.g. with list):
import itertools
import string
def alphabetic_increments():
alphabet = string.ascii_lowercase
groups = []
while True:
groups.append(alphabet)
for letters in itertools.product(*groups):
yield ''.join(letters)
Another infinite iterator:
import string
def words(alphabet):
yield from alphabet
for word in words(alphabet):
for letter in alphabet:
yield word + letter
for word in words(string.ascii_lowercase):
print(word)
Comments
In looking at your situation along with the other good answers, I thought I would throw in one other process that looks to produce the output you are after with a minimal amount of code. Following is the sample program that doesn't need any imports but just utilizes simple loops with a function that incrementally creates the ever lengthening string combinations as a proof of concept.
def print_str(text, ch, letters):
for n in letters:
print(str(text + chr(ord(ch) + n)), " ", end = "")
print()
def main():
ch = 'a' # Or uppercase "A" if uppercase letters are needed
letters = range(26)
print_str(str(""), ch, letters) # Level 1 - one character
for n in letters: # Level 2 - two characters
print_str(str(chr(ord(ch) + n)), ch, letters)
for m in letters: # Level 3 - three characters
for n in letters:
print_str(str(chr(ord(ch) + m) + chr(ord(ch) + n)), ch, letters)
main()
Following is some of the terminal output when executing this program.
craig@Vera:~/Python_Programs/Increment$ python3 Alpha.py
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 cv cw cx cy cz
da db dc dd de df dg dh di dj dk dl dm dn do dp dq dr ds dt du dv dw dx dy dz
ea eb ec ed ee ef eg eh ei ej ek el em en eo ep eq er es et eu ev ew ex ey ez
. . .
xa xb xc xd xe xf xg xh xi xj xk xl xm xn xo xp xq xr xs xt xu xv xw xx xy xz
ya yb yc yd ye yf yg yh yi yj yk yl ym yn yo yp yq yr ys yt yu yv yw yx yy yz
za zb zc zd ze zf zg zh zi zj zk zl zm zn zo zp zq zr zs zt zu zv zw zx zy zz
aaa aab aac aad aae aaf aag aah aai aaj aak aal aam aan aao aap aaq aar aas aat aau aav aaw aax aay aaz
aba abb abc abd abe abf abg abh abi abj abk abl abm abn abo abp abq abr abs abt abu abv abw abx aby abz
aca acb acc acd ace acf acg ach aci acj ack acl acm acn aco acp acq acr acs act acu acv acw acx acy acz
ada adb adc add ade adf adg adh adi adj adk adl adm adn ado adp adq adr ads adt adu adv adw adx ady adz
. . .
zua zub zuc zud zue zuf zug zuh zui zuj zuk zul zum zun zuo zup zuq zur zus zut zuu zuv zuw zux zuy zuz
zva zvb zvc zvd zve zvf zvg zvh zvi zvj zvk zvl zvm zvn zvo zvp zvq zvr zvs zvt zvu zvv zvw zvx zvy zvz
zwa zwb zwc zwd zwe zwf zwg zwh zwi zwj zwk zwl zwm zwn zwo zwp zwq zwr zws zwt zwu zwv zww zwx zwy zwz
zxa zxb zxc zxd zxe zxf zxg zxh zxi zxj zxk zxl zxm zxn zxo zxp zxq zxr zxs zxt zxu zxv zxw zxx zxy zxz
zya zyb zyc zyd zye zyf zyg zyh zyi zyj zyk zyl zym zyn zyo zyp zyq zyr zys zyt zyu zyv zyw zyx zyy zyz
zza zzb zzc zzd zze zzf zzg zzh zzi zzj zzk zzl zzm zzn zzo zzp zzq zzr zzs zzt zzu zzv zzw zzx zzy zzz
This sample program is just successively printing out the strings; however, the generated strings could be outputted to a list as an alternative. Anyway, just one more point of view and food for thought.