点特定的键和值从高度嵌套的字典



我相信一定有一种方法可以从嵌套字典中指向特定的键,而不是传统的方法。

想象一下这样的字典。

dict1 = { 'level1': "value",
'unexpectable': { 'maybe': { 'onemotime': {'name': 'John'} } } }
dict2 = { 'level1': "value", 'name': 'Steve'} 
dict3 = { 'find': { 'what': { 'you': { 'want': { 'in': { 'this': { 'maze': { 'I': { 'made': { 'for': { 'you': { 'which': { 'is in': { 'fact that': { 'was just': { 'bully your': { 'searching': { 'for': { 'the name': { 'even tho': { 'in fact': { 'actually': { 'there': { 'is': { 'in reality': { 'only': { 'one': { 'key': { 'named': { 'name': 'Michael' } } } } } } } } } } } } } } } } } } } } } } } } } } } } } }

在这种情况下,如果我们想让'name'键指向'John', 'Steve'和'Michael',那么您应该针对dict1, dict2和dict3进行不同的编码

,而我所知道的指向埋藏在嵌套字典中的键的传统方法是这样的。

print(dict1['unexpectable']['maybe']['onemotime']['name'])

,如果你不希望你的代码因为dict的空值而中断,你可能需要使用get()函数。

,我很好奇,如果我想用get()安全地获得dict1的'name',我应该这样写代码吗?

print(dict1.get('unexpectable', '').get('maybe', '').get('onemotime', '').get('name', ''))

事实上,我在运行那些get().get().get().get()的时候出错了。

请考虑是否必须打印()"name"来自那个可怕的dict3,即使它实际上只有一个键。

,想象一下你从未知的dic4中提取'name'的情况,你无法想象dic4会有什么嵌套结构。

我相信python一定有办法解决这个问题。

我在网上搜索了这个问题,但是解决方法似乎非常非常困难。

我只是想要一个简单的解决方案。

的解决方案,而不指向每个级别上的每个键。比如指向最后一个键,最重要的键。如:print(dict1.lastlevel('name')) --> 'John'

就像,不管它们有什么嵌套层结构,不管它们有多少重复项,即使它们省略了嵌套字典中间的嵌套键,这样dic17比dic16少了一级,你也可以得到你想要的,最后一级键的最后一级值。

这样的结论。

我想知道是否有一个简单的解决方案,如

打印(dict.lastlevel('名称'))

而不创建自定义函数

我想知道是否有如上的解决方案,从默认的python方法,语法,函数,逻辑或概念。

上面的解决方案可以应用于dict1, dict2, dicp3,以及任何可能出现的dict1

没有内置的方法来完成您所要求的。但是,您可以使用递归函数来挖掘嵌套字典。该函数检查所需的键是否在字典中,如果在则返回值。否则,它将遍历字典的值以查找其他字典,并扫描它们的键,直到到达底部。

dict1 = { 'level1': "value",
'unexpectable': { 'maybe': { 'onemotime': {'name': 'John'} } } }
dict2 = { 'level1': "value", 'name': 'Steve'} 
dict3 = { 'find': { 'what': { 'you': { 'want': { 'in': { 'this': { 'maze': { 'I': {
'made': { 'for': { 'you': { 'which': { 'is in': { 'fact that': { 
'was just': { 'bully your': { 'searching': { 'for': { 'the name': { 
'even tho': { 'in fact': { 'actually': { 'there': { 'is': { 'in reality': {
'only': { 'one': { 'key': { 'named': { 'name': 'Michael' 
} } } } } } } } } } } } } } } } } } } } } } } } } } } } } }

def get_nested_dict_key(d, key):
if key in d:
return d[key]
else:
for item in d.values():
if not isinstance(item, dict):
continue
return get_nested_dict_key(item, key)

print(get_nested_dict_key(dict1, 'name'))
print(get_nested_dict_key(dict2, 'name'))
print(get_nested_dict_key(dict3, 'name'))
# prints:
# John
# Steve
# Michael

您可以创建一个简单的递归生成器函数,生成每个特定键的值:

def get_nested_key(source, key):
if isinstance(source, dict):
key_value = source.get(key)
if key_value:
yield key_value
for value in source.values():
yield from get_nested_key(value, key)
elif isinstance(source, (list, tuple)):
for value in source:
yield from get_nested_key(value, key)

用法:

dictionaries = [
{'level1': 'value', 'unexpectable': {'maybe': {'onemotime': {'name': 'John'}}}},
{'level1': 'value', 'name': 'Steve'},
{'find': {'what': {'you': {'want': {'in': {'this': {'maze': {'I': {'made': {'for': {'you': {'which': {'is in': {'fact that': {'was just': {'bully your': {'searching': {'for': {'the name': {'even tho': {'in fact': {'actually': {'there': {'is': {'in reality': {'only': {'one': {'key': {'named': {'name': 'Michael'}}}}}}}}}}}}}}}}}}}}}}}}}}}}}},
{'level1': 'value', 'unexpectable': {'name': 'Alex', 'maybe': {'onemotime': {'name': 'John'}}}},
{}
]
for d in dictionaries:
print(*get_nested_key(d, 'name'), sep=', ')
输出:

John
Steve
Michael
Alex, John

最新更新