如何将路径存储到 s 的 adict
中dict
?例如,我们可以轻松地将name
值的路径存储在变量中name_field
:
person = {}
person['name'] = 'Jeff Atwood'
person['address'] = {}
person['address']['street'] = 'Main Street'
person['address']['zip'] = '12345'
person['address']['city'] = 'Miami'
# Get name
name_field = 'name'
print( person[name_field] )
如何city
存储值的路径?
# Get city
city_field = ['address', 'city']
print( person[city_field] ) // Obviously won't work!