Custom order list python -


this question has answer here:

expanding on first question here: custom sort order of list

mylist1 = ['moi.alpha', 'moi.red']
mylist2 = ['test.green', 'test.alpha','test.red']

and want sort order: ['red','green','blue','alpha']

so that: mylist1 = ['moi.red','moi.alpha']
mylist2 = ['test.red','test.green','test.alpha']

note "moi" , "test" dynamic depending on value assigned higher in code...

this same exact code last question, simple split function when looking index in sort_order:

mylist1 = ['test.alpha', 'test.green'] mylist2 = ['asdf.blue', 'asdf.alpha', 'asdf.red'] sort_order = ['red', 'blue', 'green', 'alpha'] mylist2.sort(key=lambda x: sort_order.index(x.split(".")[1])) mylist1.sort(key=lambda x: sort_order.index(x.split(".")[1])) print mylist2 print mylist1 

outputs:

['asdf.red', 'asdf.blue', 'asdf.alpha'] ['test.green', 'test.alpha'] 

all need is:

def sort_list(lst):     sort_order = ['red', 'blue', 'green', 'alpha']     lst.sort(key=lambda x: sort_order.index(x.split(".")[1]))     return lst 

Comments

Popular posts from this blog

java - Run a .jar on Heroku -

java - Jtable duplicate Rows -

validation - How to pass paramaters like unix into windows batch file -