0

I have a list of dicts that looks like this(shortened version):

[{'2022-09-18 06:00:00': '5.4'}, {'2022-09-18 06:00:00': '6.0'}, {'2022-09-18 06:00:00': '6.5'}, {'2022-09-18 09:00:00': '6.9'}, {'2022-09-18 09:00:00': '7.9'}, {'2022-09-18 09:00:00': '8.5'},]

I'm trying to separate the values that belongs to the same day and a specific time (for example those belonging to 2022-09-18 between 06:00-12:00, example of desired output:

[{'2022-09-18 06:00:00': 5.4, 6.0, 6.5, 6.9, 7.9, 8.5}]            #Between 06:00-12:00

Since I'm new to python i don't really now if there's an easy way to do this, but i thought that maybe I can define a function to separate the date and then i can loop through every part of it, but it seems like a lot of work! Here is my function

def separate_dates(dates_list):
    for i in range(len(dates)):
        time_int = dt.datetime.strptime(dates[i],"%Y-%m-%d %H:%M:%S")
        year = time_int.year
        month = time_int.month
        day = time_int.day
        hour = time_int.hour
    return time_int

is there any easy way to do this?

rail
  • 45
  • 4
  • Can you use other library such as pandas, for example? You can do something like this pd.DataFrame(dict_list, columns=['date', 'val']).groupby('date').agg(val_mean = ('val', lambda x: x.mean())) – Fariman Guliev Sep 21 '22 at 19:51
  • did you try the built-in `functools.groupy` function? – Paul H Sep 21 '22 at 19:51

0 Answers0