根据键对字典排序

对dict = {"name": "zhangshan", "sex": "man", "city": "bj"}进行排序

方法一:使用zip函数

foo = zip(dict.keys(), dict.values())
foo = [i for i in foo]
b = sorted(foo, key=lambda x: x[0])
new_dict = {i[0]: i[1] for i in b}
print(new_dict)

方法二:

1 b = sorted(dict.items(), key=lambda x: x[0])
2 new_dict = {i[0]: i[1] for i in b}
3 print(new_dict)

 

posted @ 2019-06-05 11:04  str_wjp  阅读(787)  评论(0编辑  收藏  举报