从函数[关闭]返回字典键

问题描述 投票:-1回答:1

是否有可能使函数根据参数返回嵌套的字典?

def foo(key):
    d = {'a': 1, 'b': 2, 'c': {'d': 3, 'e': 4}, }
    return d[key]

foo(['c']['d']) 

我正在等待:

3

我正在:

TypeError: list indices must be integers or slices, not str

我知道可以返回整个字典,也可以对其进行硬编码以返回字典的特定部分,但这将非常不灵活

python
1个回答
0
投票

您可以为file_unique_id创建通用的吸气剂:

def get_file_unique_id( data, property ):
     return data['photos']['file_unique_id'][property]
© www.soinside.com 2019 - 2024. All rights reserved.