python 字典排序问题

代码示例:

import string

score = {"Chinese":89, "Math":93, "English":100}

def SortedByKey():
    scored = sorted(score.items(), key=lambda x: x[0])
    print scored
    
#Values 升序排序
def SortedByValues():
    scored = sorted(score.items(), key=lambda x: x[1], reverse= True)
    print scored

#Values 降序排序
def SortedByValuesDescend():
    scored = sorted(score.items(), key=lambda x: x[1], reverse= False)
    print scored

def main_run():
    print score
    SortedByKey()
    SortedByValues()

if __name__ == "__main__":
    main_run()
结果截图:

D:\Python26\python.exe E:/Code/python/.idea/Practice.py
{'Math': 93, 'Chinese': 89, 'English': 100}
[('Chinese', 89), ('English', 100), ('Math', 93)]
[('Chinese', 89), ('Math', 93), ('English', 100)]

你可能感兴趣的:(python 字典排序问题)