Skip to content

Instantly share code, notes, and snippets.

@ViktorovEugene
Created May 17, 2016 12:47
Show Gist options
  • Save ViktorovEugene/8e0b256f735755943cf178d1d95c9930 to your computer and use it in GitHub Desktop.
Save ViktorovEugene/8e0b256f735755943cf178d1d95c9930 to your computer and use it in GitHub Desktop.
Structure inserted dict
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