Python对字典根据键值分组进行排序

In[1]: 
dic = {'drivechain_1': '前主轴承径向',
 'drivechain_10': '发电机非驱动端轴承',
 'drivechain_11': '发电机转子',
 'drivechain_2': '后主轴承径向',
 'drivechain_3': '齿轮箱内齿圈径向',
 'drivechain_4': '齿轮箱高速轴叶轮侧径向/轴向',
 'drivechain_5': '齿轮箱低速轴电机侧径向',
 'drivechain_6': '齿轮箱中间轴电机侧径向',
 'drivechain_7': '齿轮箱中间轴叶轮侧轴向',
 'drivechain_8': '齿轮箱高速轴电机侧径向',
 'drivechain_9': '发电机驱动端轴承',
 'tower_1': '塔筒横向',
 'tower_2': '塔筒纵向',
 'nacelle_1': '机舱传动链方向',
 'nacelle_2': '机舱垂直传动链方向'}

sorted(dic.items(), key=lambda x: (x[0].split('_')[0], int(x[0].split('_')[1])))
Out[1]: 
[('drivechain_1', '前主轴承径向'),
 ('drivechain_2', '后主轴承径向'),
 ('drivechain_3', '齿轮箱内齿圈径向'),
 ('drivechain_4', '齿轮箱高速轴叶轮侧径向/轴向'),
 ('drivechain_5', '齿轮箱低速轴电机侧径向'),
 ('drivechain_6', '齿轮箱中间轴电机侧径向'),
 ('drivechain_7', '齿轮箱中间轴叶轮侧轴向'),
 ('drivechain_8', '齿轮箱高速轴电机侧径向'),
 ('drivechain_9', '发电机驱动端轴承'),
 ('drivechain_10', '发电机非驱动端轴承'),
 ('drivechain_11', '发电机转子'),
 ('nacelle_1', '机舱传动链方向'),
 ('nacelle_2', '机舱垂直传动链方向'),
 ('tower_1', '塔筒横向'),
 ('tower_2', '塔筒纵向')]

 

posted @ 2020-06-19 16:03  jingsupo  阅读(93)  评论(0编辑  收藏
我要啦免费统计