3

如何将路径存储到 s 的 adictdict?例如,我们可以轻松地将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!
4

4 回答 4

5

你可以做:

path = ('address', 'city')
lookup = person
for key in path:
    lookup = lookup[key]

print lookup
# gives: Miami

KeyError如果路径的一部分不存在,这将引发 a 。

如果path由一个值组成,它也将起作用,例如('name',).

于 2013-11-06T12:40:16.183 回答
4

您可以使用reduce函数来执行此操作

print reduce(lambda x, y: x[y], city_field, person)

输出

Miami
于 2013-11-06T12:46:39.627 回答
1

使用 Simeon(和 Unubtu 已删除的答案)的替代方法是创建自己的 dict 类,该类定义了一个额外的方法:

class mydict(dict):
    def lookup(self, *args):
        tmp = self
        for field in args:
            tmp = tmp[field]
        return tmp

person = mydict()
person['name'] = 'Jeff Atwood'
person['address'] = {}
person['address']['street'] = 'Main Street'
person['address']['zip'] = '12345'
person['address']['city'] = 'Miami'

print(person.lookup('address', 'city'))
print(person.lookup('name'))
print(person.lookup('city'))

这导致:

Miami
Jeff Atwood
Traceback (most recent call last):
  File "look.py", line 17, in <module>
    print(person.lookup('city'))
  File "look.py", line 5, in lookup
    tmp = tmp[field]
KeyError: 'city'

您可以根据 thefourtheye 的建议缩短循环。如果您想真正花哨,您可能可以覆盖私有方法,例如__get__允许类似的情况person['address', 'city'],但是事情可能会变得棘手。

于 2013-11-06T12:50:22.680 回答
1

这是另一种方式 - 行为与 Simeon Visser 完全相同

from operator import itemgetter
pget = lambda map, path: reduce(lambda x,p: itemgetter(p)(x), path, map)

使用您的示例数据:

person = {
  'name': 'Jeff Atwood',
  'address': {
    'street': 'Main Street',
    'zip': '12345',
    'city': 'Miami',
  },
}

pget(person, ('address', 'zip')) # Prints '12345'
pget(person, ('name',))          # Prints 'Jeff Atwood'
pget(person, ('nope',))          # Raises KeyError
于 2013-11-06T12:53:09.817 回答