'How to create a dictionary for nested tuple values to calculate the count of data between different timeranges?

I stucked on one query I have a list of timestamps collected as:

ts = ['1649081914.478129', '1649075009.809909', '1649071215.209429',
      '1649070953.689649', '1649059574.289319', '1649057731.515459',
      '1649054673.606479', '1648844679.371929', '1648827599.979319',
      '1648789435.095729', '1648764150.271649', '1648754749.161749']

I have to create a list of dictionary items for counting the number of ts in different time ranges.

eg:

start_date = "1/4/22"
end_date = "5/4/22"

IST_TimeRange is a check on the list of timestamps above to validate if a timestamp value comes under a timewindow defined.

Similarly for other EU_Timerange and US_Timerange.

A dictionary output should look like as below:

eg:

{((1, April, 22), IST_TimeRange): 2,
  (1, April, 22), EU_TimeRange): 4,
  (1, April, 22), US_TimeRange): 2,
  (2, April, 22), IST_TimeRange): 2,
  (2, April, 22), EU_TimeRange): 6,
  (1, April, 22), US_TimeRange): 2,
  (3, April, 22), IST_TimeRange): 2,
  (3, April, 22), EU_TimeRange): 2,
  (3, April, 22), US_TimeRange): 2, ... and so on}


Solution 1:[1]

Make it vice versa. As dict can hold only primitive values, you can consider dict collisions. I suggest you to use defaultdict. Your dict will look like this: d={2: [((1, April, 22), EU_TimeRange), ((2, April, 22), IST_TimeRange)]}.

d: DefaultDict[int, List] = defaultdict(list)
d[<your key(int) value>].append(<your necessary Tuple>)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Izuki13