對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)
方法二:spa
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)