How to order list according to array values in python? -


lets have:

list = ['apple','banana','pear','orange'] values = [3,1,2,4] 

i need order list strings according values in "values" vector. in matlab have merged 2 sort vector, doesn't seem can here. ideas?

thanks!

this work you. use zip pair both lists , sort, new list.

>>> mylist = ['apple','banana','pear','orange']  >>> values = [3,1,2,4] >>> [i[1] in sorted(zip(values, mylist))]  ['banana', 'pear', 'apple', 'orange'] 

by way don't use list naming list.

you can use itertools.izip zip() except returns iterator instead of list, , therefore can better performance

>>> itertools import izip >>> mylist = ['apple','banana','pear','orange']  >>> values = [3,1,2,4] >>> [i[1] in sorted(izip(values, mylist))]  ['banana', 'pear', 'apple', 'orange'] 

Comments

Popular posts from this blog

java.util.scanner - How to read and add only numbers to array from a text file -

rewrite - Trouble with Wordpress multiple custom querystrings -