如何在列表推导中使用if else ?



我有一个字典列表:

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列表匹配显示和时间戳,这样我就可以用刚刚进来的数据替换时间戳中的成本。

例如,如果data有:'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循环。

for item in data:
show = item['show']
timestamp = item['end_time']
cost = item['cost']
for result in results:
info = result[show]
if info['timestamp'] != timestamp:
continue
result['cost'] = cost
print(results)

或者如果你想要更短的版本:

for item in data:
for result in results:
info = result[item['show']]
if info['timestamp'] != item['end_time']:
continue
result['cost'] = item['cost']
print(results)

从您提供的信息来看,data中的超时与results

不匹配。
results = [dict((key,d[key]) for d in data for key in d) for data in [[{list(results[index//len(results)].keys())[index%3]:{'timestamp':results[index//len(results)][list(results[index//len(results)].keys())[index%3]]['timestamp'],'cost':5}} if flag else {list(results[index//len(results)].keys())[index%3]:{'timestamp':results[index//len(results)][list(results[index//len(results)].keys())[index%3]]['timestamp'],'cost':results[index//len(results)][list(results[index//len(results)].keys())[index%3]]['cost']}} for index,flag in enumerate(flags)][f_i:f_i+len(results[0])] for f_i in range(0, len([{list(results[index//len(results)].keys())[index%3]:{'timestamp':results[index//len(results)][list(results[index//len(results)].keys())[index%3]]['timestamp'],'cost':5}} if flag else {list(results[index//len(results)].keys())[index%3]:{'timestamp':results[index//len(results)][list(results[index//len(results)].keys())[index%3]]['timestamp'],'cost':results[index//len(results)][list(results[index//len(results)].keys())[index%3]]['cost']}} for index,flag in enumerate(flags)]), len(results[0]))]]

相关内容

  • 没有找到相关文章

最新更新