List nested dictionaries, a dictionary sorted according to the value

Format:
data type list, a list of elements to the dictionary. A list of dictionaries by the organization.

Analysis:
You can list the dictionary first placed into a large dictionary, the entire dictionary sort after sorting is completed, the form and then converted to a list of dictionaries to

Now there is a list, sorted according to the value of each dictionary.

dict_list = [{"ming": 87}, {"mei": 93}, {"hua": 68}, {"jon": 75}, {"ston": 100}, {"jack": 56}]

Idea : You can list the dictionary first placed into a large dictionary, the entire dictionary sort after sorting is completed, the form and then converted to a list of dictionaries can be.

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 by function key-value pairs dictionary sort
the first to introduce the basic functions sorted, sorted (iterable, key, reverse ), sorted a total of iterable, key, reverse these three parameters.
Wherein the object may be iterated iterable represents, for example, may be dict.items (), dict.keys () and the like, key is a function for selecting the elements involved in the comparison, is used to specify Reverse sort order or in reverse order, Reverse = true then the reverse order, reverse order when = false is, reverse default = false.
Yaoan key value pairs dictionary sort, you can use the following statement:

>>> 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. the sorted values of the function by value dictionary sorting
to sort the value you need to use a dictionary key parameter, herein is provided a method using a primary lambda expressions, as follows:

>>> 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'})]
>>>

}), Items () method of the dictionary for the elements into tuples, and where the key parameter corresponding to the lambda expression is meant to select the second tuple element as comparison parameter (if writing key = lambda item: item [0], then the first element is selected as a comparison object, that is, as the key value comparison .lambda x: y wherein x denotes an output parameter, y represents the return value of a function of lambda), so that this method can Dictionary sort of value. The return value is a list of the sort noted that, while the list of name-value tuple pair is converted to the original dictionary.



3, using OrderDict function sort (python3 more generally applicable to the new function 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'>

Guess you like

Origin blog.51cto.com/chier11/2415974