我有以下Dict:
{'children': [{'children': [{'criteria': ['United Kingdom'],
'name': ['3'],
'prediction': ['0.256'],
'weights': ['604']},
{'criteria': ['United States'],
'name': ['4'],
'prediction': ['0.231'],
'weights': ['5316']}],
'criteria': ['United Kingdom United States'],
'name': ['2'],
'prediction': ['0.233'],
'variableNames': ['Country name'],
'weights': ['5920']},
{'children': [{'criteria': [' Brazil Canada'],
'name': ['6'],
'prediction': ['0.153'],
'weights': ['10029']},
{'criteria': ['France Germany Spain Turkey'],
'name': ['7'],
'prediction': ['0.053'],
'weights': ['1335']}],
'criteria': [' Brazil Canada France Germany Spain Turkey'],
'name': ['5'],
'prediction': ['0.141'],
'variableNames': ['Country name'],
'weights': ['11364']}],
'criteria': ['l 1'],
'name': ['1'],
'prediction': ['0.173'],
'variableNames': ['Country name'],
'weights': ['17284']}
我需要找到max&两个预测的最小值和权重,它们都是字符类型,因此需要将它们转换为Float / int,然后在迭代所有键/值时找到它们的最大/最小值。
使用一些previs很好的问题,我发现如果这些值可能有效:
max(test, key=lambda x: test[x]['prediction'])
Traceback (most recent call last):
File "<ipython-input-43-e7d4b2e19f21>", line 1, in <module>
max(test, key=lambda x: test[x]['prediction'])
File "<ipython-input-43-e7d4b2e19f21>", line 1, in <lambda>
max(test, key=lambda x: test[x]['prediction'])
TypeError: list indices must be integers, not str
这就像我将值转换为int / float:
一样接近for body in test:
test[body]['prediction'] = float(test[body]['prediction'])
test[body]['weights'] = int(test[body]['weights'])
再次引发类似的错误。
答案 0 :(得分:2)
你需要一个递归函数来遍历你的词典
def maxr(D, k):
return max([float(D[k][0])] + [maxr(i, k) for i in D.get('children', [])])
输出
>>> maxr(D, 'prediction')
0.256
您可以类似地定义函数minr
答案 1 :(得分:1)
如果您想要的只是特定字段的最大值,那么John的解决方案可以正常工作,但我认为拥有一个递归生成每个字典的生成器会更灵活。
def iter_children(data):
yield data
for child in data.get('children', []):
for c in iter_children(child): # if you're using Python3 "yield from" can simplify this
yield c
print max(float(c['prediction'][0]) for c in iter_children(raw_data))
print max(int(c['weights'][0]) for c in iter_children(raw_data))
然后,只有在&#34;巴西&#34;之后才能轻松做到最好的预测。符合标准。
print max(float(c['prediction'][0]) for c in iter_children(raw_data) if 'Brazil' in c['criteria'][0])