[ACCEPTED]-sort dictionary by another dictionary-dictionary
Accepted answer
The first code box has invalid Python syntax 4 (I suspect the d =
parts are extraneous...?) as 3 well as unwisely trampling on the built-in 2 name list
.
Anyway, given for example:
d = {'file_name':'thisfile.flt', 'item_name':'box', 'item_height':'8.7',
'item_width':'10.5', 'item_depth':'2.2', 'texture_file': 'red.jpg'}
order = {
'file_name': 0,
'item_name': 1,
'item_height': 2,
'item_width': 3,
'item_depth': 4,
'texture_file': 5
}
one nifty 1 way to get the desired result ['thisfile.flt', 'box', '8.7', '10.5', '2.2', "red.jpg']
would be:
def doit(d, order):
return [d[k] for k in sorted(order, key=order.get)]
Source:
stackoverflow.com
More Related questions
Cookie Warning
We use cookies to improve the performance of the site. By staying on our site, you agree to the terms of use of cookies.