简体   繁体   中英

Sort dictionary as per length of values

d={
    1:[1,2,3,4],
    2:[5,4],
    3:[5,6,7]
}

Given a dictionary, we need to sort the dictionary as per the length of values (in this case, as per length of lists)

Output should be as below:

{1:[1,2,3,4],3:[5,6,7],2:[5,4]}

Please help me with the code

如果您确定您的键是整数,那么简单的方法:

d = {key: value for key, value in sorted(d.items())}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM