According to the dictionary sort key

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

Method a: zip function using

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)

Method Two:

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)

 

Guess you like

Origin www.cnblogs.com/str-/p/10978420.html