Python. Separating timestamp by minute and Sorting into a new list

Viewed 65

hey guys lets say that I have a dictionary of Food types

food_types = {
    "pasta" : [],
    "Seafood" : [],
    "Chinese" : []
}

currently I have a list of timestamps that I would append to the dictionary value along with its cost

def utc_to_local(utc_dt):
    return utc_dt.replace(tzinfo=tz.gettz('UTC')).astimezone(tz.gettz('America/New_York'))

def perdelta(start, end, delta):
    curr = start
    while curr < end:
        yield curr
        curr += delta

start_time = utc_to_local(datetime.datetime.utcnow() - datetime.timedelta(minutes=120)).replace(second=0, microsecond=0)

end_time = utc_to_local(datetime.datetime.utcnow() - datetime.timedelta(minutes=5)).replace(second=0, microsecond=0)

for key, value in food_types.items():
    for result in perdelta(start_time, end_time, datetime.timedelta(minutes=1)):
        food_types[key].append({'timestamp': str(result), 'cost': 0})

this would result me getting a list of dictionaries

{
    "pasta": [
        {
            "timestamp": "2021-02-10 13:06:00-05:00",
            "cost": 0
        },
        {
            "timestamp": "2021-02-10 13:07:00-05:00",
            "cost": 0
        },
        {
            "timestamp": "2021-02-10 13:08:00-05:00",
            "cost": 0
        },
        {
            "timestamp": "2021-02-10 13:09:00-05:00",
            "cost": 0
        },
        {
            "timestamp": "2021-02-10 13:10:00-05:00",
            "cost": 0
        }
    ],
    "Seafood": [
        {
            "timestamp": "2021-02-10 13:06:00-05:00",
            "cost": 0
        },
        {
            "timestamp": "2021-02-10 13:07:00-05:00",
            "cost": 0
        },
        {
            "timestamp": "2021-02-10 13:08:00-05:00",
            "cost": 0
        },
        {
            "timestamp": "2021-02-10 13:09:00-05:00",
            "cost": 0
        },
        {
            "timestamp": "2021-02-10 13:10:00-05:00",
            "cost": 0
        }
    ],
    "Chinese": [
        {
            "timestamp": "2021-02-10 13:06:00-05:00",
            "cost": 0
        },
        {
            "timestamp": "2021-02-10 13:07:00-05:00",
            "cost": 0
        },
        {
            "timestamp": "2021-02-10 13:08:00-05:00",
            "cost": 0
        },
        {
            "timestamp": "2021-02-10 13:09:00-05:00",
            "cost": 0
        },
        {
            "timestamp": "2021-02-10 13:10:00-05:00",
            "cost": 0
        }
    ]
}

What I want to achieve is to separate timestamps per minute into its own list the timestamps

for example :

[{'pasta' : {"timestamp": "2021-02-10 13:06:00-05:00", "cost": 0} }, 'seafood' :{{"timestamp": "2021-02-10 13:06:00-05:00", "cost": 0} , 'chinese': {"timestamp": "2021-02-10 13:06:00-05:00", "cost": 0}}], [{'pasta' : {"timestamp": "2021-02-10 13:07:00-05:00", "cost": 0} }, 'seafood' :{{"timestamp": "2021-02-10 13:07:00-05:00", "cost": 0} , 'chinese': {"timestamp": "2021-02-10 13:07:00-05:00", "cost": 0}}],[{'pasta' : {"timestamp": "2021-02-10 13:08:00-05:00", "cost": 0} }, 'seafood' :{{"timestamp": "2021-02-10 13:08:00-05:00", "cost": 0} , 'chinese': {"timestamp": "2021-02-10 13:08:00-05:00", "cost": 0}}], ..., ..., ..., ..., until the end of the timestamp.

Is there a way I can achieve this ?

1 Answers

This should help you

x = sorted(x, key = lambda k: k["date"])
Related