如何从python中的字典中提取特定值 [英] How to extract a specific value from a dictionary in python
本文介绍了如何从python中的字典中提取特定值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想从Python中的Google距离矩阵API中提取距离.它返回的对象是python字典.
I want to extract distance from google distance matrix API in Python. The objet it returned is a python dictionary.
{
'destination_addresses': [Mumbai, Maharashtra, India '],
'origin_addresses': ['Powai, Mumbai, Maharashtra, India'],
'rows': [{
'elements': [{
'distance': {
'text': u '47.1 km',
'value': 47086
},
'duration': {
'text': '1 hour 17 mins',
'value': 4628
},
'status': 'OK'
}]
}],
'status': 'OK'
}
我想从上方提取距离和持续时间.我用过
I want to extract the distance and duration from above. I have used
distance = my_distance["rows"]
for a in distance:
result = a["elements"]
print result
它给了我
[{
u 'duration': {
u 'text': u '1 hour 17 mins', u 'value': 4628
}, u 'distance': {
u 'text': u '47.1 km', u 'value': 47086
}, u 'status': u 'OK'
}]
现在如何提取持续时间(1小时17分钟)和距离(47.1公里)及其对应的值(4628和47086)
Now how to extract duration (1 hour 17 mins) and distance (47.1 km) and their corresponding values (4628 & 47086)
推荐答案
您可以简单地通过以下方式进行操作:
You may simply do it as:
print "time taken : ", d["rows"][0]["elements"][0]["duration"]["text"]
print "distance : ", d["rows"][0]["elements"][0]["distance"]["text"]
print "distance value : ", d["rows"][0]["elements"][0]["distance"]["value"]
print "duration value : ", d["rows"][0]["elements"][0]["duration"]["value"]
这篇关于如何从python中的字典中提取特定值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文