fallen mcmullan
fallen mcmullan

Reputation: 85

Sort a list of strings by index

i have a text file full of symbols and numbers all as string. I am trying to move it over in another list where it sorts after the index number. But i am new at python, and not really sure how to do this. I have in my "a" made i tuple and seperated the int from the string. I now want to sort on the ints.

sortedlist = []
with open("m_scrambled.txt") as file:
    for line in file:
            a = (int(line.partition(" ")[0]),line.partition(" ")[2])
            b=sorted(a)
            print(a)

the error i am getting by trying to sort on "a".

'<' not supported between instances of 'str' and 'int'

a snippet of the file, i hope you can help me getting smarter on this!

150                                                                                                                                  
139                                        ,W@@@@@@@@@####*;;####@@@W@@@@##Wii;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;::,,,,,,,,,:::
140                                        .xWW@@W@@@@##W+;;;;+W@@WWWWW@@##Wi;i;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;::::::::::;;;::
141                                        `+@@@@@@@####Wii;;;iW###@@@@@###ni;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;::,,,...``
138                                    `,;*n@@@@@@########i:;x###@@@@######@#;iii;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;::,,..```...
146                                   .#xxxMWWWW@#######@n#++#n@#######@@WMMWWWWz;;;;;;;;;;;;;;;;;;;;;;:,.``                         
137                                 `:+zxMMMMMMMMMMWW@@##@:,,x##############W*iii;;;i;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;::,,..```   
147                                 ``.+W@@@@@@@####Wxnz++****++znxW####@@@@@#@@z*iiii;;;;;;ii;;;;:,.``                              
142                                 `````.iM############Wi;;;;*W############M+;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;:,,...``       
136                               `;+zzznnxnnnnnnnxxxxxMMMxxxMMMMMMMMMMWWWWWWWWWMxz+i;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;::,,,..``    
148                               ``.,;nW@@@@@@@@Wxz+*iii;;;;iii**+#nMW@@@@@@@@@x+*ii;;:::,,,..``                                    
149                               ```.:;+zzzz#+i:,.````  ``````````..,:;i+##zz#+;:..``                                               
135                              `*##zzzzzzzzzzznnxxMxxxxnnnnxxxxxxxxxMMxxxMMMMMMMMMMx#i;;;;;;;;;;;;;;;;;;;;;;;;;;;;;:::,,...``      
134                             `*+####zzzz#zzzznnnxxxxxnnnnnnnxxxxxxxxxxxxxxxMxxxxxMMMx#i;;;;i;;i;i;;;;;;;;;;;;::::,,,..````        
132                            :+#+####zzz#zzznzzzzzzzz#####zzznnnnnnnzzznnnnnnnxxxxxxxxxxxi`````````````````                        
145                            ``.,,,,:*xxMW@@@@#########niii*x@#######@@WMMW@@M+;;;;;;;;;;;;;;;;;;;;;;;;;;:,..`                     
133                            `i+#####zzz##zznnnnnznzzzzzzzznnnnnnnnxxnnnnxxxxxxxxxxxxxMxi:::::::::::::::,,,,,....`````             
130                           :+++####################zzzzzzzzznnnnzznnnzzzznnnzznnnnnnnnnxxx,                                       
131                           `*+#+#######zzzzz####zz#####z##zzzzzzzzzzzzznznnnnnnnnnxnnnnxxi                                        
143                           `..,::;;;;in##############@*;;;;+@#############@ni;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;::,,.```            
128                          ,****++++++++++++#######zzznnnnzzzzznnxxxnnnnnzznnnn#zxnzzznWWWWMi                                      
127                          ;i****++++++*++#z###zzzzz#zz##zzzzzzzzznnxnnnzzzznnxn#xnzznMWMxxxz`                                     

Upvotes: 0

Views: 150

Answers (1)

scotty3785
scotty3785

Reputation: 7006

I've used a regular expression to find the number at the start of each line and use this as the method for python to sort

import re

def sortByLeadingNumber(line):
    matches = re.match("^[\d]+", line)[0]
    return int(matches)

with open('scrambled_file.txt') as file:
    sortedLines = list(sorted(file.readlines(),key=sortByLeadingNumber))

print(sortedLines)

If you don't want to use regular expressions, a slightly less robust method would be the following which just splits the line up by space characters and uses the first item as a number to sort by

def sortByLeadingNumberNoRe(line):
    return int(line.split()[0])

You'd then use this as the key argument to sorted.

Upvotes: 2

Related Questions