首页 > 解决方案 > 根据值对python中的字典列表进行排序并将排序的对象添加到新列表中

问题描述

我有一个字典列表。每个字典都有两个键值,在排序时要考虑它们。一个是“col”,另一个是“row”

我想要的是

对于每个“行”键,我想获取所有对象并根据“col”的值对它们进行排序。最终列表应该明智地包含所有对象“行”并按“col”排序

例如

对于值为 1 的“行”键,我想获取所有对象并按键“col”值的升序对这些对象进行排序。

注意:col 的值范围仅从 1 到 12

我试过的

这是我尝试过的一种伪代码

for column_number in range(1,13):
    for each object in json:
        if object's "row" key is equal to column number(For each coloumn 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)

我的实际代码

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

要排序的变量:

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

**我对上述变量 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}]

任何帮助将不胜感激

标签: pythonpandassortingnumpy

解决方案


sorted

使用中的key参数sorted。确保传递一个可调用,该可调用返回要按顺序优先级排序的元素中的元组。

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

相同的答案,更明确

def f(d):
    return d['row'], d['col']

sorted(mson, key=f)

熊猫

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

推荐阅读