我有一个带有值列表的嵌套字典,我想拥有 -两个列表之间的最大索引明智值 -每个最大值的“ id”(按id的意思是来自哪个列表的值以及它的索引)。
我已经在两个列表之间有了索引明智的最大值,我需要的是'id'。
#create dictionary:
test = {}
test['A'] = {}
test['A']['number'] = [2,2,3]
test['A']['id'] = ['x','y','z']
test['B'] = {}
test['B']['number'] = [1,3,2]
test['B']['id'] = ['a','b','c']
#this the maximum index-wise value between the two lists
max_list = [max(*l) for l in zip(test['A']['number'], test['B']['number'])]
print(max_list)
我想要的是另一个包含以下内容的列表: ['x','b','z']
答案 0 :(得分:2)
制作一个id
和number
的内部邮政编码,以便我们知道哪个id属于哪个数字,
然后将max与自定义键功能一起使用(按数字),然后将它们分开:
test = {}
test['A'] = {}
test['A']['number'] = [2,2,3]
test['A']['id'] = ['x','y','z']
test['B'] = {}
test['B']['number'] = [1,3,2]
test['B']['id'] = ['a','b','c']
tuple_list = [max(*l, key=lambda t: t[1]) for l in zip(zip(test['A']['id'],test['A']['number']), zip(test['B']['id'],test['B']['number']))]
max_num_list = [t[1] for t in tuple_list]
max_id_list = [t[0] for t in tuple_list]
print(max_num_list)
print(max_id_list)
输出:
[2, 3, 3]
['x', 'b', 'z']