[Tutor] Sorting a List

Emile van Sebille emile at fenx.com
Thu Jan 13 02:41:19 CET 2011


On 1/12/2011 4:56 PM Corey Richardson said...
> 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')])

I might use use glob in two steps:

(  sorted([image for image in glob.glob('walk_?.png')])
  + sorted([image for image in glob.glob('walk_??.png')])
)


Emile


>
> 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.
>
> Thanks,
> Corey Richardson
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>




More information about the Tutor mailing list