I have a list made like this
[1,2,3,4,...]
with every number associated with a timestamp like
[1231469744, 1231470173, 1231470988, 1231471428,...]
What is the best way to divide the first list taking monthly interval of the second?
For example if the first three object are in November they should be in the same list, etc. etc.
CodePudding user response:
You can map them:
from datetime import datetime
from collections import defaultdict
timestamps = [1231469744, 1231470173, 1231470988, 1231471428]
months_dict = defaultdict(list)
for timestamp in timestamps:
months_dict[datetime.fromtimestamp(timestamp).month].append(timestamp)
print(dict(months_dict))
Output:
{1: [1231469744, 1231470173, 1231470988, 1231471428], 12: [1545730073]}