On Wed, Jan 12, 2011, Corey Richardson wrote: >Hello Tutors, > >I am generating XML definitions for animations to be used in a >FIFE-based game. I need to sort the frames of the animations, and I am >currently using: >sorted([image for image in os.listdir(path) if image.endswith('.png')]) > >The final output in the XML is: > > <frame source="walk_0.png"/> > <frame source="walk_1.png"/> > <frame source="walk_10.png"/> > <frame source="walk_11.png"/> > <frame source="walk_2.png"/> > <frame source="walk_3.png"/> > <frame source="walk_4.png"/> > <frame source="walk_5.png"/> > <frame source="walk_6.png"/> > <frame source="walk_7.png"/> > <frame source="walk_8.png"/> > <frame source="walk_9.png"/> > >Having frame 10 and 11 between frame 1 and 2 is not desired behavior; >how can I sort all of these with walk_10.png and company (this isn't the >only animation like this) being after walk_9.png? A google search >doesn't return anything I could use. I tried just using >[image for image in os.listdir(path) if image.endswith('.png')], >but that doesn't appear to have any order.
This is discussed in the Python Cookbook from O'Reilly. If I remember correctly, it deals exactly with the problem of sorting file names containing numbers as you have above. Here's a bit of code I use in a module I wrote for dealing with RPM versions. import re _digits = re.compile(r'(\d+)') def ver(s): r = _digits.split(s) r[1::2] = map(lambda x: int(x), r[1::2]) return(tuple(r)) class FileInfo(object): def __init__(self, fname) self.fname = fname self.cmp = ver(fname) def __cmp__(self, othr): return cmp(self.cmp, othr.cmp) Bill -- INTERNET: b...@celestial.com Bill Campbell; Celestial Software LLC URL: http://www.celestial.com/ PO Box 820; 6641 E. Mercer Way Voice: (206) 236-1676 Mercer Island, WA 98040-0820 Fax: (206) 232-9186 Skype: jwccsllc (206) 855-5792 Government is the great fiction, through which everbody endeavors to live at the expense of everybody else. -- Frederic Bastiat _______________________________________________ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor