简体   繁体   English

Python过滤并从字典列表中创建字典

[英]Python filter and create dict from list of dict

I have list of dict 我有字典的清单

[{
    'bytesUsed': 79095,
    'servicePlan': '25MB',
    'smsUsed': 0,
    'source': 'Raw Usage',
    'timestamp': '2019-02-28T19:00:00-05:00'
}, {
    'bytesUsed': 310435,
    'servicePlan': '25MB',
    'smsUsed': 0,
    'source': 'Raw Usage',
    'timestamp': '2019-04-01T19:00:00-05:00'
}, {
    'bytesUsed': 286033,
    'servicePlan': '25MB',
    'smsUsed': 0,
    'source': 'Raw Usage',
    'timestamp': '2019-05-06T19:00:00-05:00'
}]

Which I want to filter out dict if timestamp field is not in the range (2019-02-11T19:00:00-05:00 - 2019-04-10T19:00:00-05:00) and create key:value map like 如果timestamp字段不在范围内(2019-02-11T19:00:00-05:00-2019-04-10T19:00:00-05:00),我想过滤出dict并创建key:value映射喜欢

{'2019-02-28T19:00:00-05:00' : 79095, '2019-04-01T19:00:00-05:00' : 310435} 

Whats is the efficient way to do this ? 有效的方法是什么?

at present I am doing it for & if on each dict in list, which it taking time consuming. 目前,我正在针对&if在列表中的每个字典上执行此操作,这非常耗时。

I took at guess at what you're trying get. 我猜到你正在尝试得到什么。 Unfortunately your expected output wasn't clear. 不幸的是,您的预期输出不清楚。

I'm always a fan of dateutil module. 我一直是dateutil模块的粉丝。

from dateutil.parser import parse
dt1 = parse("2019-02-11T19:00:00-05:00")
dt2 = parse("2019-04-10T19:00:00-05:00")

newdl = []
for dic in dictlist:
    dt = parse(dic['timestamp'])
    if dt1 <= dt <= dt2:
        newdl.append({dic['timestamp'] : dic['bytesUsed']})
print(newdl)

output: 输出:

[{'2019-02-28T19:00:00-05:00': 79095}, {'2019-04-01T19:00:00-05:00': 310435}]

If you think you are going to use the datetimes again, I would make dt the key instead of the string; 如果您认为您将要再次使用日期时间,那么我将dt为键,而不是字符串。 it will save you from having to parse the string again later. 这样可以避免以后再解析该字符串。

Though I would be careful using dates of any kind (string or datetime) as dictionary keys unless you can guarantee they are always unique. 虽然我会小心地使用任何类型的日期(字符串或日期时间)作为字典键,除非您可以保证它们始终是唯一的。

Try this, 尝试这个,

from dateutil.parser import parse

start_dt = parse("2019-02-11T19:00:00-05:00")
end_dt = parse("2019-04-10T19:00:00-05:00")

arr = [
    {
        'bytesUsed': 79095,
        'servicePlan': '25MB',
        'smsUsed': 0,
        'source': 'Raw Usage',
        'timestamp': '2019-02-28T19:00:00-05:00'
    },
    {
        'bytesUsed': 310435,
        'servicePlan': '25MB',
        'smsUsed': 0,
        'source': 'Raw Usage',
        'timestamp': '2019-04-01T19:00:00-05:00'
    },
    {
        'bytesUsed': 286033,
        'servicePlan': '25MB',
        'smsUsed': 0,
        'source': 'Raw Usage',
        'timestamp': '2019-05-06T19:00:00-05:00'
    }
]

result = {i['timestamp']: i['bytesUsed'] for i in arr if start_dt <= parse(i['timestamp']) <= end_dt}
print(result)

output: 输出:

{'2019-02-28T19:00:00-05:00': 79095, '2019-04-01T19:00:00-05:00': 310435}

If you don't have dateutil module, can isntall using pip install python-dateutil command. 如果没有dateutil模块,可以使用pip install python-dateutil命令pip install python-dateutil Really didn't get your output, if this is not you expected, please add a comment to this answer. 确实没有得到您的输出,如果这不是您期望的,请在此答案中添加评论。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM