如何在列表理解中使用if-else?

2024-06-07 14:42:43 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一份字典清单:

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?或者更确切地说是我的问题的另一个选择


Tags: thein列表fordatatimeshowtimestamp
2条回答
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]))]]

在这种情况下,我不会使用列表理解,而是使用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中的超时不匹配

相关问题 更多 >

    热门问题