Created
May 17, 2016 12:47
-
-
Save ViktorovEugene/8e0b256f735755943cf178d1d95c9930 to your computer and use it in GitHub Desktop.
Structure inserted dict
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import datetime | |
import random | |
import json | |
def get_rand_dates_list(amount): | |
return [ | |
datetime.datetime(random.randint(2010, 2016), random.randint(1, 12), 1) | |
for i in range(amount)] | |
def structure_dates(dates_list): | |
result = {} | |
for date in dates_list: | |
year = result.setdefault(date.year, {}) | |
month = year.setdefault(date.month, []) | |
month.append(str(date)) | |
result = [[k, list(v.items())] for k, v in result.items()] | |
return result | |
if __name__ == '__main__': | |
structured_dates = structure_dates(get_rand_dates_list(12)) | |
print(json.dumps({'data': structured_dates}, indent=4)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment