我有一份字典清单:
data = [{'end_time': 1628565660, 'show': 'mad.men', 'cost': 0.0023123688 },
{'end_time': 1628565780, 'show': 'the.sopranos', 'cost': 0.0023123688},
{'end_time': 1628565780, 'show': 'breaking.bad', 'cost': 1.0667859907}]
这里有一个嵌套字典列表:
[{'mad.men': {'timestamp': '1628566560', 'cost': 0},
'breaking.bad': {'timestamp': '1628566560', 'cost': 0},
'the.sopranos': {'timestamp': '1628566560', 'cost': 0}},
{'mad.men': {'timestamp': '1628566620', 'cost': 0},
'breaking.bad': {'timestamp': '1628566620', 'cost': 0},
'the.sopranos': {'timestamp': '1628566620', 'cost': 0}},
{'mad.men': {'timestamp': '1628566680', 'cost': 0},
'breaking.bad': {'timestamp': '1628566680', 'cost': 0},
'the.sopranos': {'timestamp': '1628566680', 'cost': 0}},
{'mad.men': {'timestamp': '1628566740', 'cost': 0},
'breaking.bad': {'timestamp': '1628566740', 'cost': 0},
'the.sopranos': {'timestamp': '1628566740', 'cost': 0}},
{'mad.men': {'timestamp': '1628566800', 'cost': 0},
'breaking.bad': {'timestamp': '1628566800', 'cost': 0},
'the.sopranos': {'timestamp': '1628566800', 'cost': 0}},
{'mad.men': {'timestamp': '1628566860', 'cost': 0},
'breaking.bad': {'timestamp': '1628566860', 'cost': 0},
'the.sopranos': {'timestamp': '1628566860', 'cost': 0}}]
我试图在这里实现的是迭代我的data
列表,并且results
列表匹配显示和时间戳,这样我就可以用刚进来的数据替换该时间戳内的成本
例如,如果数据具有:
'the.sopranos': {'timestamp': '1628566860', 'cost': 5.00
结果列表与时间戳匹配,结果中的此值:
'the.sopranos': {'timestamp': '1628566560', 'cost': 0}
转化为
'the.sopranos': {'timestamp': '1628566560', 'cost': 5.00}
这就是我迄今为止所做的尝试:
for item in data:
dt_object = datetime.datetime.fromtimestamp(item['end_time'] / 1000.0).replace(second=0, microsecond=0).strftime('%s')
for k, v in [(k, v) for x in results for (k, v) in x.items() if k == item['show'] and dt_object == v['timestamp']]:
print(k,v)
但它什么也不打印。我如何在列表中实现这样的if–else?或者更确切地说是我的问题的另一个选择
在这种情况下,我不会使用列表理解,而是使用for循环
或者,如果您想要较短的版本:
根据您提供的信息,
data
中的超时与results
中的超时不匹配相关问题 更多 >
编程相关推荐