将排序列表重新映射到字典中

时间:2014-12-16 06:40:18

标签: python sorting dictionary list-comprehension

我有一个映射到支持者ID的页面ID的词典。我将它们拆分为名为avatarsnon_avatars的列表,因为我希望它们按照具有化身的列表和没有(分别)的列表进行排序,我还想在['user']['supported_date']上对它们进行排序。 / p>

这是我原来的数据字典:

pages = {
    274: {
        1307: {
            'support': 1,
            'profile_id': 1307,
            'id': 16,
            'user': {
                'first_name': 'robot10',
                'photo': '/static/images/avatars/avatar-male-100.png',
                'supported_date': '2014-11-18 19:56:35.961452+00:00',
                'id': 1307,
            },
        },
        1298: {
            'support': 1,
            'profile_id': 1298,
            'id': 19,
            'user': {
                'first_name': 'robot1',
                'photo': '/user_photos/01/f2/01f236a6a15fee6153f834010deafffa.png',
                'supported_date': '2014-12-12 22:28:35.262345+00:00',
                'id': 1298,
            },
        },
        1299: {
            'support': 1,
            'profile_id': 1299,
            'id': 24,
            'user': {
                'first_name': 'robot2',
                'photo': '/user_photos/da/ea/daea1c07bc50568fab0462327b66c251.png',
                'supported_date': '2014-12-12 22:28:54.382168+00:00',
                'id': 1299,
            },
        },
        1300: {
            'support': 1,
            'profile_id': 1300,
            'id': 29,
            'user': {
                'first_name': 'robot3',
                'photo': '/static/images/avatars/avatar-male-100.png',
                'supported_date': '2014-12-12 22:30:53.800588+00:00',
                'id': 1300,
            },
        },
        1302: {
            'support': 1,
            'profile_id': 1302,
            'id': 35,
            'user': {
                'first_name': 'robot5',
                'photo': '/user_photos/24/35/24351b2c4cf99723c7a04b9bbf5706cb.png',
                'supported_date': '2014-12-12 22:31:14.376383+00:00',
                'id': 1302,
            },
        },
    },
    347: {1298: {
        'support': 1,
        'profile_id': 1298,
        'id': 23,
        'user': {
            'first_name': 'robot1',
            'photo': '/user_photos/01/f2/01f236a6a15fee6153f834010deafffa.png',
            'supported_date': '2014-12-12 22:28:38.751230+00:00',
            'id': 1298,
        },
    }, 1299: {
        'support': 1,
        'profile_id': 1299,
        'id': 28,
        'user': {
            'first_name': 'robot2',
            'photo': '/user_photos/da/ea/daea1c07bc50568fab0462327b66c251.png',
            'supported_date': '2014-12-12 22:30:04.421848+00:00',
            'id': 1299,
        },
    }, 1300: {
        'support': 1,
        'profile_id': 1300,
        'id': 33,
        'user': {
            'first_name': 'robot3',
            'photo': '/static/images/avatars/avatar-male-100.png',
            'supported_date': '2014-12-12 22:30:55.968285+00:00',
            'id': 1300,
        },
    }},
    348: {
        1298: {
            'support': 1,
            'profile_id': 1298,
            'id': 20,
            'user': {
                'first_name': 'robot1',
                'photo': '/user_photos/01/f2/01f236a6a15fee6153f834010deafffa.png',
                'supported_date': '2014-12-12 22:28:35.855000+00:00',
                'id': 1298,
            },
        },
        1299: {
            'support': 1,
            'profile_id': 1299,
            'id': 25,
            'user': {
                'first_name': 'robot2',
                'photo': '/user_photos/da/ea/daea1c07bc50568fab0462327b66c251.png',
                'supported_date': '2014-12-12 22:28:56.569903+00:00',
                'id': 1299,
            },
        },
        1300: {
            'support': 1,
            'profile_id': 1300,
            'id': 30,
            'user': {
                'first_name': 'robot3',
                'photo': '/static/images/avatars/avatar-male-100.png',
                'supported_date': '2014-12-12 22:30:54.204695+00:00',
                'id': 1300,
            },
        },
        1302: {
            'support': 1,
            'profile_id': 1302,
            'id': 36,
            'user': {
                'first_name': 'robot5',
                'photo': '/user_photos/24/35/24351b2c4cf99723c7a04b9bbf5706cb.png',
                'supported_date': '2014-12-12 22:31:19.885209+00:00',
                'id': 1302,
            },
        },
    },
    278: {
        1298: {
            'support': 1,
            'profile_id': 1298,
            'id': 21,
            'user': {
                'first_name': 'robot1',
                'photo': '/user_photos/01/f2/01f236a6a15fee6153f834010deafffa.png',
                'supported_date': '2014-12-12 22:28:36.780765+00:00',
                'id': 1298,
            },
        },
        1299: {
            'support': 1,
            'profile_id': 1299,
            'id': 26,
            'user': {
                'first_name': 'robot2',
                'photo': '/user_photos/da/ea/daea1c07bc50568fab0462327b66c251.png',
                'supported_date': '2014-12-12 22:28:57.598403+00:00',
                'id': 1299,
            },
        },
        1300: {
            'support': 1,
            'profile_id': 1300,
            'id': 31,
            'user': {
                'first_name': 'robot3',
                'photo': '/static/images/avatars/avatar-male-100.png',
                'supported_date': '2014-12-12 22:30:54.979874+00:00',
                'id': 1300,
            },
        },
        1302: {
            'support': 1,
            'profile_id': 1302,
            'id': 34,
            'user': {
                'first_name': 'robot5',
                'photo': '/user_photos/24/35/24351b2c4cf99723c7a04b9bbf5706cb.png',
                'supported_date': '2014-12-12 22:31:12.505020+00:00',
                'id': 1302,
            },
        },
    },
    279: {1298: {
        'support': 1,
        'profile_id': 1298,
        'id': 22,
        'user': {
            'first_name': 'robot1',
            'photo': '/user_photos/01/f2/01f236a6a15fee6153f834010deafffa.png',
            'supported_date': '2014-12-12 22:28:37.330745+00:00',
            'id': 1298,
        },
    }, 1299: {
        'support': 1,
        'profile_id': 1299,
        'id': 27,
        'user': {
            'first_name': 'robot2',
            'photo': '/user_photos/da/ea/daea1c07bc50568fab0462327b66c251.png',
            'supported_date': '2014-12-12 22:28:58.149790+00:00',
            'id': 1299,
        },
    }, 1300: {
        'support': 1,
        'profile_id': 1300,
        'id': 32,
        'user': {
            'first_name': 'robot3',
            'photo': '/static/images/avatars/avatar-male-100.png',
            'supported_date': '2014-12-12 22:30:55.475817+00:00',
            'id': 1300,
        },
    }},
}

然后我遍历页面dict,用化身拉出支持者,并将它们放入自己的列表中。我也将没有头像的人放入他们自己的名单。

avatars = []
non_avatars = []
for pid, page_supporters in pages.items():
    for supporter_id, page_supporter in page_supporters.items():
        page_supporter['page_id'] = pid
        if 'user_photos' in page_supporter['user']['photo']:
            avatars.append(page_supporter)
        else:
            non_avatars.append(page_supporter)

现在我按用户支持的日期对它们进行排序。

# Sort all avatars by user supported date
avatars = sorted(avatars, key=lambda supporter: supporter['user']['supported_date'], reverse=True)

# Do the same for the non avatars
non_avatars = sorted(non_avatars, key=lambda supporter: supporter['user']['supported_date'], reverse=True)

最后将它们组合成一个列表。

# These are now supported by those who have avatars first and their supported date DESC
combined_supporters = avatars + non_avatars

从这里开始,我需要重新创建原始字典。我尝试update,但它一直覆盖以前的值。我想我需要使用setdefault,但我不完全确定。

如何将combined_supporters列表重新设置为与pages dict相同的结构?

2 个答案:

答案 0 :(得分:3)

如果您需要维护订购,则必须使用collections.OrderedDict() instances替换您的网页词典;这些轨道插入顺序和所有迭代然后重复该顺序:

from collections import OrderedDict

# replace existing pages dict with a new one maintaining ordering
pages = {page_id: OrderedDict() for page_id in pages}

for supporter in combined_supporters:
    page_id = supporter.pop('page_id')
    pages[page_id][supporter['profile_id']] = supporter

根据您的设置,这会产生:

>>> from collections import OrderedDict
>>> pages = {page_id: OrderedDict() for page_id in pages}
>>> for supporter in combined_supporters:
...     page_id = supporter.pop('page_id')
...     pages[page_id][supporter['profile_id']] = supporter
... 
>>> from pprint import pprint
>>> pprint(pages)
{274: OrderedDict([(1302, {'support': 1, 'profile_id': 1302, 'id': 35, 'user': {'photo': '/user_photos/24/35/24351b2c4cf99723c7a04b9bbf5706cb.png', 'first_name': 'robot5', 'supported_date': '2014-12-12 22:31:14.376383+00:00', 'id': 1302}}), (1299, {'support': 1, 'profile_id': 1299, 'id': 24, 'user': {'photo': '/user_photos/da/ea/daea1c07bc50568fab0462327b66c251.png', 'first_name': 'robot2', 'supported_date': '2014-12-12 22:28:54.382168+00:00', 'id': 1299}}), (1298, {'support': 1, 'profile_id': 1298, 'id': 19, 'user': {'photo': '/user_photos/01/f2/01f236a6a15fee6153f834010deafffa.png', 'first_name': 'robot1', 'supported_date': '2014-12-12 22:28:35.262345+00:00', 'id': 1298}}), (1300, {'support': 1, 'profile_id': 1300, 'id': 29, 'user': {'photo': '/static/images/avatars/avatar-male-100.png', 'first_name': 'robot3', 'supported_date': '2014-12-12 22:30:53.800588+00:00', 'id': 1300}}), (1307, {'support': 1, 'profile_id': 1307, 'id': 16, 'user': {'photo': '/static/images/avatars/avatar-male-100.png', 'first_name': 'robot10', 'supported_date': '2014-11-18 19:56:35.961452+00:00', 'id': 1307}})]),
 278: OrderedDict([(1302, {'support': 1, 'profile_id': 1302, 'id': 34, 'user': {'photo': '/user_photos/24/35/24351b2c4cf99723c7a04b9bbf5706cb.png', 'first_name': 'robot5', 'supported_date': '2014-12-12 22:31:12.505020+00:00', 'id': 1302}}), (1299, {'support': 1, 'profile_id': 1299, 'id': 26, 'user': {'photo': '/user_photos/da/ea/daea1c07bc50568fab0462327b66c251.png', 'first_name': 'robot2', 'supported_date': '2014-12-12 22:28:57.598403+00:00', 'id': 1299}}), (1298, {'support': 1, 'profile_id': 1298, 'id': 21, 'user': {'photo': '/user_photos/01/f2/01f236a6a15fee6153f834010deafffa.png', 'first_name': 'robot1', 'supported_date': '2014-12-12 22:28:36.780765+00:00', 'id': 1298}}), (1300, {'support': 1, 'profile_id': 1300, 'id': 31, 'user': {'photo': '/static/images/avatars/avatar-male-100.png', 'first_name': 'robot3', 'supported_date': '2014-12-12 22:30:54.979874+00:00', 'id': 1300}})]),
 279: OrderedDict([(1299, {'support': 1, 'profile_id': 1299, 'id': 27, 'user': {'photo': '/user_photos/da/ea/daea1c07bc50568fab0462327b66c251.png', 'first_name': 'robot2', 'supported_date': '2014-12-12 22:28:58.149790+00:00', 'id': 1299}}), (1298, {'support': 1, 'profile_id': 1298, 'id': 22, 'user': {'photo': '/user_photos/01/f2/01f236a6a15fee6153f834010deafffa.png', 'first_name': 'robot1', 'supported_date': '2014-12-12 22:28:37.330745+00:00', 'id': 1298}}), (1300, {'support': 1, 'profile_id': 1300, 'id': 32, 'user': {'photo': '/static/images/avatars/avatar-male-100.png', 'first_name': 'robot3', 'supported_date': '2014-12-12 22:30:55.475817+00:00', 'id': 1300}})]),
 347: OrderedDict([(1299, {'support': 1, 'profile_id': 1299, 'id': 28, 'user': {'photo': '/user_photos/da/ea/daea1c07bc50568fab0462327b66c251.png', 'first_name': 'robot2', 'supported_date': '2014-12-12 22:30:04.421848+00:00', 'id': 1299}}), (1298, {'support': 1, 'profile_id': 1298, 'id': 23, 'user': {'photo': '/user_photos/01/f2/01f236a6a15fee6153f834010deafffa.png', 'first_name': 'robot1', 'supported_date': '2014-12-12 22:28:38.751230+00:00', 'id': 1298}}), (1300, {'support': 1, 'profile_id': 1300, 'id': 33, 'user': {'photo': '/static/images/avatars/avatar-male-100.png', 'first_name': 'robot3', 'supported_date': '2014-12-12 22:30:55.968285+00:00', 'id': 1300}})]),
 348: OrderedDict([(1302, {'support': 1, 'profile_id': 1302, 'id': 36, 'user': {'photo': '/user_photos/24/35/24351b2c4cf99723c7a04b9bbf5706cb.png', 'first_name': 'robot5', 'supported_date': '2014-12-12 22:31:19.885209+00:00', 'id': 1302}}), (1299, {'support': 1, 'profile_id': 1299, 'id': 25, 'user': {'photo': '/user_photos/da/ea/daea1c07bc50568fab0462327b66c251.png', 'first_name': 'robot2', 'supported_date': '2014-12-12 22:28:56.569903+00:00', 'id': 1299}}), (1298, {'support': 1, 'profile_id': 1298, 'id': 20, 'user': {'photo': '/user_photos/01/f2/01f236a6a15fee6153f834010deafffa.png', 'first_name': 'robot1', 'supported_date': '2014-12-12 22:28:35.855000+00:00', 'id': 1298}}), (1300, {'support': 1, 'profile_id': 1300, 'id': 30, 'user': {'photo': '/static/images/avatars/avatar-male-100.png', 'first_name': 'robot3', 'supported_date': '2014-12-12 22:30:54.204695+00:00', 'id': 1300}})])}

重新格式化输出:

pages = {
    274: OrderedDict([
        (
            1302, {
                'support': 1,
                'profile_id': 1302,
                'id': 35, 
                'user': {
                    'photo': '/user_photos/24/35/24351b2c4cf99723c7a04b9bbf5706cb.png',
                    'first_name': 'robot5', 
                    'supported_date': '2014-12-12 22:31:14.376383+00:00',
                    'id': 1302
                }
            }
        ), 
        (
            1299, {
                'support': 1,
                'profile_id': 1299,
                'id': 24,
                'user': {
                    'photo': '/user_photos/da/ea/daea1c07bc50568fab0462327b66c251.png',
                    'first_name': 'robot2',
                    'supported_date': '2014-12-12 22:28:54.382168+00:00',
                    'id': 1299
                }
            }
        ),
        (
            1298, {
                'support': 1,
                'profile_id': 1298,
                'id': 19, 
                'user': {
                    'photo': '/user_photos/01/f2/01f236a6a15fee6153f834010deafffa.png',
                    'first_name': 'robot1',
                    'supported_date': '2014-12-12 22:28:35.262345+00:00',
                    'id': 1298
                }
            }
        ), 
        (
            1300, {
                'support': 1,
                'profile_id': 1300,
                'id': 29,
                'user': {
                    'photo': '/static/images/avatars/avatar-male-100.png',
                    'first_name': 'robot3',
                    'supported_date': '2014-12-12 22:30:53.800588+00:00',
                    'id': 1300
                }
            }
        ), 
        (
            1307, {
                'support': 1,
                'profile_id': 1307,
                'id': 16, 
                'user': {
                    'photo': '/static/images/avatars/avatar-male-100.png',
                    'first_name': 'robot10',
                    'supported_date': '2014-11-18 19:56:35.961452+00:00',
                    'id': 1307
                }
            }
        )
    ]),
    278: OrderedDict([
        (
            1302, {
                'support': 1,
                'profile_id': 1302,
                'id': 34,
                'user': {
                    'photo': '/user_photos/24/35/24351b2c4cf99723c7a04b9bbf5706cb.png',
                    'first_name': 'robot5',
                    'supported_date': '2014-12-12 22:31:12.505020+00:00',
                    'id': 1302
                }
            }
        ),
        (
            1299, {
                'support': 1,
                'profile_id': 1299,
                'id': 26,
                'user': {
                    'photo': '/user_photos/da/ea/daea1c07bc50568fab0462327b66c251.png',
                    'first_name': 'robot2',
                    'supported_date': '2014-12-12 22:28:57.598403+00:00',
                    'id': 1299
                }
            }
        ),
        (
            1298, {
                'support': 1,
                'profile_id': 1298,
                'id': 21,
                'user': {
                    'photo': '/user_photos/01/f2/01f236a6a15fee6153f834010deafffa.png',
                    'first_name': 'robot1',
                    'supported_date': '2014-12-12 22:28:36.780765+00:00',
                    'id': 1298
                }
            }
        ),
        (
            1300, {
                'support': 1,
                'profile_id': 1300,
                'id': 31,
                'user': {
                    'photo': '/static/images/avatars/avatar-male-100.png',
                    'first_name': 'robot3',
                    'supported_date': '2014-12-12 22:30:54.979874+00:00',
                    'id': 1300
                }
            }
        )
    ]),
    279: OrderedDict([
        (
            1299, {
                'support': 1,
                'profile_id': 1299,
                'id': 27,
                'user': {
                    'photo': '/user_photos/da/ea/daea1c07bc50568fab0462327b66c251.png',
                    'first_name': 'robot2',
                    'supported_date': '2014-12-12 22:28:58.149790+00:00',
                    'id': 1299
                }
            }
        ),
        (
            1298, {
                'support': 1,
                'profile_id': 1298,
                'id': 22,
                'user': {
                    'photo': '/user_photos/01/f2/01f236a6a15fee6153f834010deafffa.png',
                    'first_name': 'robot1',
                    'supported_date': '2014-12-12 22:28:37.330745+00:00',
                    'id': 1298
                }
            }
        ),
        (
            1300, {
                'support': 1,
                'profile_id': 1300,
                'id': 32,
                'user': {
                    'photo': '/static/images/avatars/avatar-male-100.png',
                    'first_name': 'robot3',
                    'supported_date': '2014-12-12 22:30:55.475817+00:00',
                    'id': 1300
                }
            }
        )
    ]),
    347: OrderedDict([
        (
             1299, {
                'support': 1,
                'profile_id': 1299,
                'id': 28,
                'user': {
                    'photo': '/user_photos/da/ea/daea1c07bc50568fab0462327b66c251.png',
                    'first_name': 'robot2',
                    'supported_date': '2014-12-12 22:30:04.421848+00:00',
                    'id': 1299
                }
            }
        ),
        (
            1298, {
                'support': 1,
                'profile_id': 1298,
                'id': 23,
                'user': {
                    'photo': '/user_photos/01/f2/01f236a6a15fee6153f834010deafffa.png',
                    'first_name': 'robot1',
                    'supported_date': '2014-12-12 22:28:38.751230+00:00',
                    'id': 1298
                }
            }
        ),
        (
            1300, {
                'support': 1,
                'profile_id': 1300,
                'id': 33,
                'user': {
                    'photo': '/static/images/avatars/avatar-male-100.png',
                    'first_name': 'robot3',
                    'supported_date': '2014-12-12 22:30:55.968285+00:00',
                    'id': 1300
                }
            }
        )
    ]),
    348: OrderedDict([
        (
            1302, {
                'support': 1,
                'profile_id': 1302,
                'id': 36,
                'user': {
                    'photo': '/user_photos/24/35/24351b2c4cf99723c7a04b9bbf5706cb.png',
                    'first_name': 'robot5',
                    'supported_date': '2014-12-12 22:31:19.885209+00:00',
                    'id': 1302
                }
            }
        ),
        (
            1299, {
                'support': 1,
                'profile_id': 1299,
                'id': 25,
                'user': {
                    'photo': '/user_photos/da/ea/daea1c07bc50568fab0462327b66c251.png',
                    'first_name': 'robot2',
                    'supported_date': '2014-12-12 22:28:56.569903+00:00',
                    'id': 1299
                }
            }
        ),
        (
            1298, {
                'support': 1,
                'profile_id': 1298,
                'id': 20,
                'user': {
                    'photo': '/user_photos/01/f2/01f236a6a15fee6153f834010deafffa.png',
                    'first_name': 'robot1',
                    'supported_date': '2014-12-12 22:28:35.855000+00:00',
                    'id': 1298
                }
            }
        ),
        (
            1300, {
                'support': 1,
                'profile_id': 1300,
                'id': 30,
                'user': {
                    'photo': '/static/images/avatars/avatar-male-100.png',
                    'first_name': 'robot3',
                    'supported_date': '2014-12-12 22:30:54.204695+00:00',
                    'id': 1300
                }
            }
        )
    ])
}

答案 1 :(得分:2)

因为python词典不是"排序"实体的一个技巧是将数据保存在字典中,然后使用由您正在排序的项目组成的排序列表,以及获取您正在寻找的其余数据所需的密钥或访问方法。请注意,在python中排序时,您可以选择提供 cmp 运算符和函数,该函数允许等效_sortBy