pythonpandassortingnumpy

Sort list of dictionary in python based on values and add the sorted objects to a new list


I have a list of dictionary.Each dictionary has two key values which are to be considered while sorting.One is "col" and other is "row"

What I want

For each "row" key I want to get all the objects and sort them on the value of "col".And the final list should have all the objects "row" wise and sorted on "col"

For example

For "row" key with value 1 I want to get all the objects and sort those objects in ascending order of value of key "col".

Note: value of col ranges from 1 to 12 only

What I tried

This is a kind of pseudo-code of what I have tried

for column_number in range(1,13):
    for each object in json:
        if object's "row" key is equal to column number(For each column get all of its object):
            add_the_objects_of_each_column_to_a_list
        sort_the_list
        add_the_sorted_list_to_a_new_list(this list should be similar in form as the input)

My actual code

list_to_sort = []
newlist = []
sorted_list = []


for col_number in range(1,13):
    for obj in mson:    
        if(obj['row'] == col_number):
            list_to_sort.append(obj)
        newlist = sorted(list_to_sort, key=lambda k: k['col'])
        
#I am not able to write below this for how I will place this sorted newlist in my 

final sorted_list variable which is the final variable I want having row wise objects which are sorted on column

The Variable which is to be sorted:

mson = [
    {'col': 10, 'row': 1, 'size_x': 3, 'size_y': 3},
    {'col': 1, 'row': 1, 'size_x': 3, 'size_y': 2},
    {'col': 5, 'row': 1, 'size_x': 2, 'size_y': 2},
    {'col': 1, 'row': 3, 'size_x': 3, 'size_y': 2},
    {'col': 1, 'row': 5, 'size_x': 2, 'size_y': 2},
    {'col': 1, 'row': 7, 'size_x': 3, 'size_y': 2},
    {'col': 8, 'row': 4, 'size_x': 3, 'size_y': 3.0},
    {'col': 6, 'row': 7, 'size_x': 3, 'size_y': 2}]

**My Desired Output for above variable mson **

mson_sorted = [
    {'col': 1, 'row': 1, 'size_x': 3, 'size_y': 2},
    {'col': 5, 'row': 1, 'size_x': 2, 'size_y': 2},
    {'col': 10, 'row': 1, 'size_x': 3, 'size_y': 3},
    {'col': 1, 'row': 3, 'size_x': 3, 'size_y': 2},
    {'col': 8, 'row': 4, 'size_x': 3, 'size_y': 3.0},
    {'col': 1, 'row': 5, 'size_x': 2, 'size_y': 2},
    {'col': 1, 'row': 7, 'size_x': 3, 'size_y': 2},
    {'col': 6, 'row': 7, 'size_x': 3, 'size_y': 2}]

Any help will be really appreciated


Solution

  • sorted

    Use the key argument in sorted. Make sure to pass a callable that returns a tuple in the elements you want to sort by in the order priority.

    sorted(mson, key=lambda d: (d['row'], d['col']))
    
    [{'col': 1, 'row': 1, 'size_x': 3, 'size_y': 2},
     {'col': 5, 'row': 1, 'size_x': 2, 'size_y': 2},
     {'col': 10, 'row': 1, 'size_x': 3, 'size_y': 3},
     {'col': 1, 'row': 3, 'size_x': 3, 'size_y': 2},
     {'col': 8, 'row': 4, 'size_x': 3, 'size_y': 3.0},
     {'col': 1, 'row': 5, 'size_x': 2, 'size_y': 2},
     {'col': 1, 'row': 7, 'size_x': 3, 'size_y': 2},
     {'col': 6, 'row': 7, 'size_x': 3, 'size_y': 2}]
    

    Same answer, more explicit

    def f(d):
        return d['row'], d['col']
    
    sorted(mson, key=f)
    

    Pandas

    pd.DataFrame(mson, dtype=object).sort_values(['row', 'col']).to_dict('r')
    
    [{'col': 1, 'row': 1, 'size_x': 3, 'size_y': 2},
     {'col': 5, 'row': 1, 'size_x': 2, 'size_y': 2},
     {'col': 10, 'row': 1, 'size_x': 3, 'size_y': 3},
     {'col': 1, 'row': 3, 'size_x': 3, 'size_y': 2},
     {'col': 8, 'row': 4, 'size_x': 3, 'size_y': 3.0},
     {'col': 1, 'row': 5, 'size_x': 2, 'size_y': 2},
     {'col': 1, 'row': 7, 'size_x': 3, 'size_y': 2},
     {'col': 6, 'row': 7, 'size_x': 3, 'size_y': 2}]