如何在列表理解中使用 if else?
How to utilize if else in a list comprehension?
我有一个词典列表:
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)
但它只打印 nothing.How 我可以在列表理解中实现 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]))]]
我有一个词典列表:
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)
但它只打印 nothing.How 我可以在列表理解中实现 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]))]]