【问题标题】:Python. Separating timestamp by minute and Sorting into a new listPython。按分钟分隔时间戳并排序到新列表中
【发布时间】:2021-05-14 11:55:25
【问题描述】:

大家好,可以说我有一本食物类型的字典

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

目前我有一个时间戳列表,我会将其附加到字典值及其成本

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})

这会让我得到一个字典列表

{
    "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
        }
    ]
}

我想要实现的是将每分钟的时间戳分成自己的时间戳列表

例如:

[{'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.

有什么方法可以实现吗?

【问题讨论】:

    标签: python list datetime


    【解决方案1】:

    这应该对你有帮助

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

    【讨论】:

    • 看起来我在这里按时间戳排序,但是如何获取列表中每一分钟的时间戳,直到最后一分钟?
    • 我认为这对我的问题没有多大帮助。
    猜你喜欢
    • 2021-07-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-18
    • 2020-08-09
    • 2018-10-12
    相关资源
    最近更新 更多