sorting - In python, how would I sort a list of strings where the location of the string comparison changes? -


i have list of strings have 2 dashes separating text like:

wednesday-morning-go bowling sunday-really late @ night-sleep july-noon-bbq 

i'd sort list in alphabetical order in python last part of string--the 2nd dash , on. there way in python? e.g. want list after sorting.

july-noon-bbq wednesday-morning-go bowling sunday-really late @ night-sleep 

(i'm using python 2.6.)

you can use key attribute list.sort():

a = ["wednesday-morning-go bowling", "sunday-really late @ night-sleep",      "july-noon-bbq"] a.sort(key=lambda x: x.split("-", 2)[-1]) print 

prints

['july-noon-bbq', 'wednesday-morning-go bowling', 'sunday-really late @ night-sleep'] 

note split() call allows more 2 dashes. every dash after second 1 ignored , included in third part.


Comments

Popular posts from this blog

php - What is the difference between $_SERVER['PATH_INFO'] and $_SERVER['ORIG_PATH_INFO']? -

fortran - Function return type mismatch -

queue - mq_receive: message too long -