python中字典顺序逆转

 

>>> test1 = dict(key8="ccc",key3="ete",key2="iii",key4 = "wwww")
>>> type(test1)
<class 'dict'>
>>> len(test1)
4
>>> for i,j in test1.items():
    print(i,j)

    
key8 ccc
key3 ete
key2 iii
key4 wwww
>>> for i,j in reversed(test1.items()):
    print(i,j)

    
key4 wwww
key2 iii
key3 ete
key8 ccc
>>> test2 = {}
>>> for i,j in reversed(test1.items()):
    test2[i] = j

    
>>> test2
{'key4': 'wwww', 'key2': 'iii', 'key3': 'ete', 'key8': 'ccc'}
>>> test1
{'key8': 'ccc', 'key3': 'ete', 'key2': 'iii', 'key4': 'wwww'}

 

posted @ 2021-01-01 18:00  小鲨鱼2018  阅读(668)  评论(0编辑  收藏  举报