[Tutor] Sorting a list in an add order

Alan Gauld alan.gauld at freenet.co.uk
Wed Sep 6 19:36:15 CEST 2006


Matt,

> However, I want to process them so that they are arranged in the 
> correct order, which means I need to sort the list of files. Of 
> course, because they aren't named in any (obvious) order, I'm a bit 
> stuck.
>
> I thought about using a dictionary to map names and order: so 
> {"OAF":1, "Valuation":2...etc}, but I don't don't know how to take 
> it on from here.

If you are going to have to put the names in a dictionary you
don't need glob. Just oput them in a list and iterate over that list!

But I'd probably go back to the source and rename the files with
a sequence number in front:

01OAF
02Valuation

etc

Its a manual one time process but allows simple mechanised
processing thereafter. You could maybe even write a script to
autonumber any new files added...

> .startswith() to get some basic rough-matching capacity) and then 
> using that to return the order that the files should be handled in.

If you know the order and are going to have to manually type
that into Python then its pointless using glob. Just use the list you 
create.

Alan Gauld
Author of the Learn to Program web site
http://www.freenetpages.co.uk/hp/alan.gauld 



More information about the Tutor mailing list