import re the_list = ["B1","B3","B2","A1","A12","A2","AA112","A3","BB12","BA999999","C1"] #a list where I will dump my dictionaries the_pieces = [] #for each item in the list create a dictionary of the pagename,numberpart, and letter part. for i in the_list: temp = {} temp["pagename"] = i #might need to be a substring of i temp["digits"] = int(re.findall('\d+', temp["pagename"])[0]) temp["letters"] = temp["pagename"].replace(str(temp["digits"]),"") #convert the letter part to base 26 and put it into the dictionary for sorting pow26 = len(temp["letters"])-1 ajb1 = 0 for j in temp["letters"]: ajb1 += (ord(j)-64) * (26**pow26) pow26 -= 1 temp["letters_as_base26"] = int(ajb1) the_pieces.append(temp) #nested sorted gives you what you need. the_pieces = sorted(the_pieces, key=lambda k: k['digits']) the_pieces = sorted(the_pieces, key=lambda k: k['letters_as_base26']) for i in the_pieces: print i
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question