python字典排序
程序员文章站
2023-12-29 13:14:16
1.合成字典2.按键值排序3.reverse=False升序4.key=lambda x:x[1]5.dict1.items()6.dict(d_order)In [1]: a=range(1,11)In [2]: b=range(21,11,-1)In [3]: bOut[3]: range(21, 11, -1)In [4]: list(b)Out[4]: [21, 20, 19, 18, 17, 16, 15, 14, 13, 12]In [5]: dict1=dict...
1.合成字典
2.按键值排序
3.reverse=False升序
4.key=lambda x:x[1]
5.dict1.items()
6.dict(d_order)
In [1]: a=range(1,11)
In [2]: b=range(21,11,-1)
In [3]: b
Out[3]: range(21, 11, -1)
In [4]: list(b)
Out[4]: [21, 20, 19, 18, 17, 16, 15, 14, 13, 12]
In [5]: dict1=dict(zip(a,b))
In [6]: dict1
Out[6]: {1: 21, 2: 20, 3: 19, 4: 18, 5: 17, 6: 16, 7: 15, 8: 14, 9: 13, 10: 12}
In [7]: dict1.items
Out[7]: <function dict.items>
In [8]: dict1.iteritems
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-8-f967eb54c173> in <module>
----> 1 dict1.iteritems
AttributeError: 'dict' object has no attribute 'iteritems'
In [9]: d_order=sorted(dict1.items(),key=lambda x:x[1],reverse=False)
In [10]: d_order
Out[10]:
[(10, 12),
(9, 13),
(8, 14),
(7, 15),
(6, 16),
(5, 17),
(4, 18),
(3, 19),
(2, 20),
(1, 21)]
In [11]: dict(d_order)
Out[11]: {10: 12, 9: 13, 8: 14, 7: 15, 6: 16, 5: 17, 4: 18, 3: 19, 2: 20, 1: 21}
In [12]:
本文地址:https://blog.csdn.net/sinat_23971513/article/details/107877296