python - Re-mapping a sorted list into a dictionary -
i've got dict of page ids mapped supporter ids. split them lists called avatars
, non_avatars
, because want them sorted avatars, , without (respectively), , want sort them on ['user']['supported_date']
.
here original data dict:
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, }, }}, }
then loop through pages dict, pull out supporters avatars, , put them own list. put without avatars own list.
avatars = [] non_avatars = [] pid, page_supporters in pages.items(): 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)
and sort them user supported date.
# sort avatars user supported date avatars = sorted(avatars, key=lambda supporter: supporter['user']['supported_date'], reverse=true) # same non avatars non_avatars = sorted(non_avatars, key=lambda supporter: supporter['user']['supported_date'], reverse=true)
finally combine them 1 list.
# these supported have avatars first , supported date desc combined_supporters = avatars + non_avatars
from here, need re-create original dictionary. tried update
kept overwriting previous values. think need use setdefault
i'm not entirely certain.
how can turn combined_supporters
list same structure pages
dict?
if need maintain ordering, you'll have replace page dictionaries collections.ordereddict()
instances; these track insertion order , iteration repeats order:
from collections import ordereddict # replace existing pages dict new 1 maintaining ordering pages = {page_id: ordereddict() page_id in pages} supporter in combined_supporters: page_id = supporter.pop('page_id') pages[page_id][supporter['profile_id']] = supporter
given setup, produces:
>>> collections import ordereddict >>> pages = {page_id: ordereddict() page_id in pages} >>> supporter in combined_supporters: ... page_id = supporter.pop('page_id') ... pages[page_id][supporter['profile_id']] = supporter ... >>> 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}})])}
reformatting output little:
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 } } ) ]) }
Comments
Post a Comment