Colección de recortes y cosas útiles, simpáticas, interesantes(o las 3!) de Matías Herranz.
8 de enero de 2014
Custom sorting function as parameter to sorted() in Python
So, I am working with Python and I come across a list of image names, and I'd like to have them sorted. So, I call the sorted function with this list:
HA! It doesn't work as I would have liked it to. So, what can we do to achieve the sorting of the list as we want it?
Python's standard library sorted function, as you can see here, has an optional cmp parameter, through which you can pass a custom comparison function.
Going back to my example, I needed the images to be compared by the number in their name. I made this little function to achieve this goal:
And now let's put altogether:
TA DA!
Suscribirse a:
Enviar comentarios (Atom)
No hay comentarios:
Publicar un comentario