根据值对python中的字典列表进行排序,并将排序后的对象添加到新列表中

新秀_123

我有一个字典列表。每个字典都有两个在排序时要考虑的键值。一个是“ 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}]

任何帮助将不胜感激

海盗

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

排序列表并将排序后的值添加到bash中的新列表中

根据列表中的值对字典进行排序

在列表中对Python字典对象进行排序

根据值中列表的值进行字典排序

访问用户定义对象中的字典值并将它们添加到列表中

根据嵌套字典中的值对列表进行排序

在Python中,给定字典中包含值列表的字典,如何根据该列表中的项目数量对字典进行排序?

如何根据 Python 中的日期时间对字典列表进行排序

根据字典中的“依赖项”对python列表进行排序

如何对列表中的字典值进行排序?

将列表中的值添加到python中的字典

从 Python 中的列表将值添加到字典中

根据对象字段的分组对列表进行排序,根据组中的最大值进行排序

Python-在列表的字典中对值进行排序

Python将新的键/值添加到键匹配值的字典列表中

在python中将新值作为字典值添加到列表中

在Python 3中对字典列表进行排序

在Python列表中对嵌套字典进行排序?

在字典中对列表进行排序

如何根据字典内列表中的值对字典进行排序并创建字典

根据Python中的另一个字典列表对字典列表进行排序

如何根据字典值对列表进行排序?

按列表中的值对嵌套字典列表进行排序

对包含列表的字典列表中的值进行排序

根据字典中对象的对应ID对对象列表进行排序

在Python 3.7中一步就初始化并将值添加到字典中的列表

创建表并根据字典中的列表对数据进行排序

根据列表中的第ith个元素对字典进行排序

在python中对字典列表列表进行排序