遍历字典列表,如果其他值具有特定属性,则获取特定值



I "decoded"字典的字典,归结为:

waycategory_values = data['features'][0]['properties']['extras']['waycategory']['summary']

的结果是:

[{'value': 3.0, 'distance': 230472.2, 'amount': 96.03}, {'value': 0.0, 'distance': 8713.7, 'amount': 3.63}, {'value': 1.0, 'distance': 811.1, 'amount': 0.34}]

现在我想获得距离并将相应的值相加,如果值= 1.0,2.0或3.0我尝试通过以下操作获取距离:

for distance in waycategory_values[0:]['distance']:
if waycategory_values[0:]['value'] == 1.0 or 2.0 or 3.0:
print(distance)

嗯,这显然是错误的…但是我不知道怎么做——如果你能给我一个提示,那就太棒了!

data = [{'value': 3.0, 'distance': 230472.2, 'amount': 96.03}, {'value': 0.0, 'distance': 8713.7, 'amount': 3.63}, {'value': 1.0, 'distance': 811.1, 'amount': 0.34}]
total_distance = 0
# just go through the list, no need for indexing
for dataset in data:
if dataset['value'] in [1.0, 2.0, 3.0]: # <- list of values to check against
total_distance += dataset['distance']
d=[{'value': 3.0, 'distance': 230472.2, 'amount': 96.03}, {'value': 0.0, 'distance': 8713.7, 'amount': 3.63}, {'value': 1.0, 'distance': 811.1, 'amount': 0.34}]
sum(v['distance'] for v in d if v['value'] == 1 or v['value'] == 2 or   v['value'] ==  3  )
231283.30000000002  

相关内容

  • 没有找到相关文章

最新更新