Off topic:sort dictionary in python?
0
0
Entering edit mode
5.5 years ago
xupbuy ▴ 30

I have a dictionary:

d = {'b':1, 'a':3, 'aa':12,'c':24, 'bb':31,'abc':123}

I have a list containing keys:

l = ['a','c','b','abc','bb','aa']

How to sort d1 based on l in python (or pandas)? I want the keys in d1 to be sorted in the order in l.

d_sorted = {'a':3,'c':24, 'b':1,'abc':123, bb':31,'aa':12'}

Thank you.

python • 527 views
ADD COMMENT
This thread is not open. No new answers may be added
Traffic: 4031 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6