Python中字典按照指定列表的顺序排列
字典:"info":{"VP":"80003","HRM":"F140019","MP":"F130008","HRGDL2":"F140390","OWNER":"F1...
字典:
"info": {"VP": "80003", "HRM": "F140019", "MP": "F130008", "HRGDL2": "F140390", "OWNER": "F133255", "RCOM": "F900151", "GM": "00903", "HP": "80355", "HRH": "81453"}
列表:
"roles": ["OWNER", "RCOM", "HRGDL2", "HRM", "HRH", "MP", "HP", "VP", "GM"]
想要的效果:
"info": {"OWNER": "F133255", "RCOM": "F900151","HRGDL2": "F140390","HRM": "F140019","HRH": "81453",
"MP": "F130008", "HP": "80355","VP": "80003", "GM": "00903"} 展开
"info": {"VP": "80003", "HRM": "F140019", "MP": "F130008", "HRGDL2": "F140390", "OWNER": "F133255", "RCOM": "F900151", "GM": "00903", "HP": "80355", "HRH": "81453"}
列表:
"roles": ["OWNER", "RCOM", "HRGDL2", "HRM", "HRH", "MP", "HP", "VP", "GM"]
想要的效果:
"info": {"OWNER": "F133255", "RCOM": "F900151","HRGDL2": "F140390","HRM": "F140019","HRH": "81453",
"MP": "F130008", "HP": "80355","VP": "80003", "GM": "00903"} 展开
1个回答
展开全部
roles = ["OWNER", "RCOM", "HRGDL2", "HRM", "HRH", "MP", "HP", "VP", "GM"]
info = {"VP": "80003", "HRM": "F140019", "MP": "F130008", "HRGDL2": "F140390", "OWNER": "F133255", "RCOM": "F900151", "GM": "00903", "HP": "80355", "HRH": "81453"}
sorted_dict = map(lambda x:{x:info[x]}, roles)
print(sorted_dict)
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询