我有如下列表的字典:
{
'FR-8_20190502_MD_Case1': [11595, 8250, 13023, 7223],
'FR-8_20190505_MD_Case1': [11595, 8250, 13023, 7418],
'FR-8_20190507_MD_Case1': [11595, 8250, 13023, 7223],
'FR-8_20190509_MD_Case1': [11595, 8250, 13023, 7384],
'FR-8_20190508_MD_Case1': [12948, 8250, 13023, 7223],
'FR-8_20190506_MD_Case1': [12056, 8250, 13023, 7223]
}
我想列出这些列表中每个索引的最大值,例如:
[12948, 8250, 13023, 7418]
这是我目前所拥有的,它有效,但我很确定它可以改进:(ima_sizes 是我的列表字典)
max_sizes = [0, 0, 0, 0]
for k, v in ima_sizes.items():
for i in range(4):
if v[i] > max_sizes[i]:
max_sizes[i] = v[i]
是否有更 pythonic 的方法来实现它?
请您参考如下方法:
使用 zip() 转置值并使用 max() 找到最大值:
d = {
'FR-8_20190502_MD_Case1': [11595, 8250, 13023, 7223],
'FR-8_20190505_MD_Case1': [11595, 8250, 13023, 7418],
'FR-8_20190507_MD_Case1': [11595, 8250, 13023, 7223],
'FR-8_20190509_MD_Case1': [11595, 8250, 13023, 7384],
'FR-8_20190508_MD_Case1': [12948, 8250, 13023, 7223],
'FR-8_20190506_MD_Case1': [12056, 8250, 13023, 7223]
}
print([max(v) for v in zip(*d.values())])
打印:
[12948, 8250, 13023, 7418]
