格式:
数据类型为列表, 列表元素为字典. 由字典组织的列表形式.
分析:
可以将列表中的字典先放入到一个大字典中, 对整个字典进行排序, 在排序完成后, 再转换为列表包含字典的形式即可
现在有一个列表, 根据每个字典的值进行排序.
dict_list = [{"ming": 87}, {"mei": 93}, {"hua": 68}, {"jon": 75}, {"ston": 100}, {"jack": 56}]
思路: 可以将列表中的字典先放入到一个大字典中, 对整个字典进行排序, 在排序完成后, 再转换为列表包含字典的形式即可.
- from operator import itemgetter
- dict_list = [{
- "ming": 87
- }, {
- "mei": 93
- }, {
- "hua": 68
- }, {
- "jon": 75
- }, {
- "ston": 100
- }, {
- "jack": 56
- }]
- mid_dict = {
- key: value for x in dict_list for key, value in x.items()
- }
- mid_list = sorted(mid_dict.items(), key=itemgetter(1))
- fin_list = [{
- x[0]: x[1]
- } for x in mid_list]
例子: T=[{'xgei-0/0/1/1': '9'}, {'xgei-0/0/1/2': '20'},{'xgei-0/0/1/15': '12'}]
- def Sorted_listdict(dict_list ):
- New_List=[]
- New_D={}
- mid_dict = {key: value for x in dict_list for key, value in x.items()} ### 格式写法. 列表与字典的结构
- #print (mid_dict)
- ordered_dict = OrderedDict(sorted(mid_dict.items(), key=lambda t: int(t[1]), reverse=True))
- #print (type(ordered_dict),ordered_dict)
- #<class 'collections.OrderedDict'> OrderedDict([('xgei-0/0/1/2', '20'), ('xgei-0/0/1/15', '12'), ('xgei-0/0/1/1', '9')])
- for x in ordered_dict:
- New_D[x]=mid_dict[x]
- New_List.append(New_D)
- print (New_List)
- return New_List
1.sorted 函数按 key 值对字典排序
先来基本介绍一下 sorted 函数, sorted(iterable,key,reverse),sorted 一共有 iterable,key,reverse 这三个参数.
其中 iterable 表示可以迭代的对象, 例如可以是 dict.items(),dict.keys()等, key 是一个函数, 用来选取参与比较的元素, reverse 则是用来指定排序是倒序还是顺序, reverse=true 则是倒序, reverse=false 时则是顺序, 默认时 reverse=false.
要按 key 值对字典排序, 则可以使用如下语句:
- >>> dict2={
- ... '11': {
- 'RX': '-11', 'TX': '-11'
- },
- ... '1': {
- 'RX': '-1', 'TX': '-1'
- },
- ... '2': {
- 'RX': '-2', 'TX': '-2'
- },
- ... '3': {
- 'RX': '-3', 'TX': '-3'
- },
- ... '20': {
- 'RX': '-20', 'TX': '-20'
- },
- ... '4': {
- 'RX': '-4', 'TX': '-4'
- },
- ... '5': {
- 'RX': '-5', 'TX': '-5'
- },
- ... '30': {
- 'RX': '-30', 'TX': '-30'
- },
- ... '6': {
- 'RX': '-6', 'TX': '-6'
- },
- ... '7': {
- 'RX': '-7', 'TX': '-7'
- }
- ...
- }
- >>> sorted(dict2.keys())
- ['1', '11', '2', '20', '3', '30', '4', '5', '6', '7']
直接使用 sorted(d.keys())就能按 key 值对字典排序, 这里是按照顺序对 key 值排序的, 如果想按照倒序排序的话, 则只要将 reverse 置为 true 即可.
2.sorted 函数按 value 值对字典排序
要对字典的 value 排序则需要用到 key 参数, 在这里主要提供一种使用 lambda 表达式的方法, 如下:
- >>> sorted(dict2.items(),key=lambda i:i[0])
- [('1', {
- 'RX': '-1', 'TX': '-1'
- }), ('11', {
- 'RX': '-11', 'TX': '-11'
- }), ('2', {
- 'RX': '-2', 'TX': '-2'
- }), ('20', {
- 'RX': '-20', 'TX': '-20'
- }), ('3', {
- 'RX': '-3', 'TX': '-3'
- }), ('30', {
- 'RX': '-30', 'TX': '-30'
- }), ('4', {
- 'RX': '-4', 'TX': '-4'
- }), ('5', {
- 'RX': '-5', 'TX': '-5'
- }), ('6', {
- 'RX': '-6', 'TX': '-6'
- }), ('7', {
- 'RX': '-7', 'TX': '-7'
- })]
- >>> sorted(dict2.items(),key=lambda i:int(i[0]))
- [('1', {
- 'RX': '-1', 'TX': '-1'
- }), ('2', {
- 'RX': '-2', 'TX': '-2'
- }), ('3', {
- 'RX': '-3', 'TX': '-3'
- }), ('4', {
- 'RX': '-4', 'TX': '-4'
- }), ('5', {
- 'RX': '-5', 'TX': '-5'
- }), ('6', {
- 'RX': '-6', 'TX': '-6'
- }), ('7', {
- 'RX': '-7', 'TX': '-7'
- }), ('11', {
- 'RX': '-11', 'TX': '-11'
- }), ('20', {
- 'RX': '-20', 'TX': '-20'
- }), ('30', {
- 'RX': '-30', 'TX': '-30'
- })]
- >>>
这里的 d.items()实际上是将 d 转换为可迭代对象,
迭代对象的元素为 ('1', {'RX': '-1', 'TX': '-1'}), ('2', {'RX': '-2', 'TX': '-2'}), ('3', {'RX': '-3', 'TX': '-3'}), ('4', {'RX': '-4', 'TX': '-4'}), ('5', {'RX': '-5', 'TX': '-5'}), ('6', {'RX': '-6', 'TX': '-6'}), ('7', {'RX': '-7', 'TX': '-7'}), ('11', {'RX': '-11', 'TX': '-11'}), ('20', {'RX': '-20', 'TX': '-20'}), ('30', {'RX': '-30', 'TX': '-30'}),items()方法将字典的元素 转化为了元组,
而这里 key 参数对应的 lambda 表达式的意思则是选取元组中的第二个元素作为比较参数 (如果写作 key=lambda item:item[0] 的话则是选取第一个元素作为比较对象,
也就是 key 值作为比较对象. lambda x:y 中 x 表示输出参数, y 表示 lambda 函数的返回值), 所以采用这种方法可以对字典的 value 进行排序.
注意排序后的返回值是一个 list, 而原字典中的名值对被转换为了 list 中的元组.
3, 使用 OrderDict 函数排序(一般适用于 python3 以上, 该函数为 3 新增)
- >>> dict1={
- ... '11': {
- 'RX': '-11', 'TX': '-11'
- },
- ... '1': {
- 'RX': '-1', 'TX': '-1'
- },
- ... '2': {
- 'RX': '-2', 'TX': '-2'
- },
- ... '3': {
- 'RX': '-3', 'TX': '-3'
- },
- ... '20': {
- 'RX': '-20', 'TX': '-20'
- },
- ... '4': {
- 'RX': '-4', 'TX': '-4'
- },
- ... '5': {
- 'RX': '-5', 'TX': '-5'
- },
- ... '30': {
- 'RX': '-30', 'TX': '-30'
- },
- ... '6': {
- 'RX': '-6', 'TX': '-6'
- },
- ... '7': {
- 'RX': '-7', 'TX': '-7'
- }
- ...
- }
- >>> from collections import OrderedDict
- >>> ordered_dict = OrderedDict(sorted(dict1.items(), key=lambda t: t[0]))
- >>> print (ordered_dict)
- OrderedDict([('1', {
- 'RX': '-1', 'TX': '-1'
- }), ('11', {
- 'RX': '-11', 'TX': '-11'
- }), ('2', {
- 'RX': '-2', 'TX': '-2'
- }), ('20', {
- 'RX': '-20', 'TX': '-20'
- }), ('3', {
- 'RX': '-3', 'TX': '-3'
- }), ('30', {
- 'RX': '-30', 'TX': '-30'
- }), ('4', {
- 'RX': '-4', 'TX': '-4'
- }), ('5', {
- 'RX': '-5', 'TX': '-5'
- }), ('6', {
- 'RX': '-6', 'TX': '-6'
- }), ('7', {
- 'RX': '-7', 'TX': '-7'
- })])
- >>> new_ordered_dict = OrderedDict(sorted(dict1.items(), key=lambda t: int(t[0])))
- >>> print (new_ordered_dict)
- OrderedDict([('1', {
- 'RX': '-1', 'TX': '-1'
- }), ('2', {
- 'RX': '-2', 'TX': '-2'
- }), ('3', {
- 'RX': '-3', 'TX': '-3'
- }), ('4', {
- 'RX': '-4', 'TX': '-4'
- }), ('5', {
- 'RX': '-5', 'TX': '-5'
- }), ('6', {
- 'RX': '-6', 'TX': '-6'
- }), ('7', {
- 'RX': '-7', 'TX': '-7'
- }), ('11', {
- 'RX': '-11', 'TX': '-11'
- }), ('20', {
- 'RX': '-20', 'TX': '-20'
- }), ('30', {
- 'RX': '-30', 'TX': '-30'
- })])
- >>> type(ordered_dict) ## 返回有序字典
- <class 'collections.OrderedDict'>
来源: http://www.bubuko.com/infodetail-3110494.html