使用 Python 返回键嵌套字典
Returning keys nested dictionary with Python
编辑:感谢@arzo 的捕获
我有一个嵌套字典,结构如下:
{
"key1":"string",
"key2":[{"nestedkey":"nestedvalue"}],
"key3":[1,2,3],
"key4":[{"nestedlevel1key":[{"nestedlevel2key":"nestedlevel2value"}]}],
"key5": {},
"key6": {"regularkey": "regularvalue"},
"key7": 15
}
其中字典键可以包含:
- 字符串
- 整数
- 词典列表
- 包含词典列表的词典列表
- 空字典
- 普通字典
问题陈述正在优化一种可以处理返回字典中所有键的方法。我可以这样写:
def get_keys(dict_example):
keys = []
for k,v in dict_example.items():
keys.append(k)
if isinstance(v, dict):
for k in v.keys():
keys.append(k)
if isinstance(v, list):
if isinstance(v[0], dict):
for k,v in v[0].items():
keys.append(k)
if isinstance(v, list) and isinstance(v[0], dict):
for k in v[0].keys():
keys.append(k)
return keys
keys = get_keys(dict_example)
print(keys)
这会给我(无特定顺序)键列表:
['key1', 'key2', 'nestedkey1', 'key3', 'key4', 'nestedlevel1key', 'nestedlevel2key', 'key5', 'key6', 'regularkey', 'key7']
但我不确定是否有一种优化的方法可以简化以处理所有 6 个用例,这些用例也可以横向穿过数组,而不管有多少级别。现在我对级别数进行了启发式计算,但我需要考虑这个数组中的更深级别。
他们只有 2 种情况需要处理:list
和 dict
因为它们包含其他东西,然后使用递归
- 对于
dict
:保留键,并搜索值
- for
list
:搜索值
def get_keys(item):
keys = []
if isinstance(item, dict):
for k, v in item.items():
keys.append(k)
keys.extend(get_keys(v))
elif isinstance(item, (list, tuple)):
for x in item:
keys.extend(get_keys(x))
return keys
values = {
"key1": "string", "key2": [{"nestedkey": "nestedvalue"}], "key3": [1, 2, 3],
"key4": [{"nestedlevel1key": [{"nestedlevel2key": "nestedlevel2value"}]}],
"key5": {}, "key6": {"regularkey": "regularvalue"},
}
keys = get_keys(values)
print(keys)
# ['key1', 'key2', 'nestedkey', 'key3', 'key4', 'nestedlevel1key', 'nestedlevel2key', 'key5', 'key6', 'regularkey']
你可以用递归函数解决这个问题,而不是大量的嵌套循环:
origDict = {
"key1":"string",
"key2":[{"nestedkey":"nestedvalue"}],
"key3":[1,2,3],
"key4":[{"nestedlevel1key":[{"nestedlevel2key":"nestedlevel2value"}]}],
"key5": {},
"key6": {"regularkey": "regularvalue"},
"key7": 15
}
keyList = []
def get_keys(inDict, listOfKeys):
for k, v in inDict.items():
listOfKeys.append(k)
if isinstance(v, list):
for item in v:
if isinstance(item, dict):
get_keys(item, listOfKeys)
elif isinstance(v, dict):
get_keys(v, listOfKeys)
get_keys(origDict, keyList)
print(keyList)
这会打印:
['key1', 'key2', 'nestedkey', 'key3', 'key4', 'nestedlevel1key',
'nestedlevel2key', 'key5', 'key6', 'regularkey', 'key7']```
编辑:感谢@arzo 的捕获
我有一个嵌套字典,结构如下:
{
"key1":"string",
"key2":[{"nestedkey":"nestedvalue"}],
"key3":[1,2,3],
"key4":[{"nestedlevel1key":[{"nestedlevel2key":"nestedlevel2value"}]}],
"key5": {},
"key6": {"regularkey": "regularvalue"},
"key7": 15
}
其中字典键可以包含:
- 字符串
- 整数
- 词典列表
- 包含词典列表的词典列表
- 空字典
- 普通字典
问题陈述正在优化一种可以处理返回字典中所有键的方法。我可以这样写:
def get_keys(dict_example):
keys = []
for k,v in dict_example.items():
keys.append(k)
if isinstance(v, dict):
for k in v.keys():
keys.append(k)
if isinstance(v, list):
if isinstance(v[0], dict):
for k,v in v[0].items():
keys.append(k)
if isinstance(v, list) and isinstance(v[0], dict):
for k in v[0].keys():
keys.append(k)
return keys
keys = get_keys(dict_example)
print(keys)
这会给我(无特定顺序)键列表:
['key1', 'key2', 'nestedkey1', 'key3', 'key4', 'nestedlevel1key', 'nestedlevel2key', 'key5', 'key6', 'regularkey', 'key7']
但我不确定是否有一种优化的方法可以简化以处理所有 6 个用例,这些用例也可以横向穿过数组,而不管有多少级别。现在我对级别数进行了启发式计算,但我需要考虑这个数组中的更深级别。
他们只有 2 种情况需要处理:list
和 dict
因为它们包含其他东西,然后使用递归
- 对于
dict
:保留键,并搜索值 - for
list
:搜索值
def get_keys(item):
keys = []
if isinstance(item, dict):
for k, v in item.items():
keys.append(k)
keys.extend(get_keys(v))
elif isinstance(item, (list, tuple)):
for x in item:
keys.extend(get_keys(x))
return keys
values = {
"key1": "string", "key2": [{"nestedkey": "nestedvalue"}], "key3": [1, 2, 3],
"key4": [{"nestedlevel1key": [{"nestedlevel2key": "nestedlevel2value"}]}],
"key5": {}, "key6": {"regularkey": "regularvalue"},
}
keys = get_keys(values)
print(keys)
# ['key1', 'key2', 'nestedkey', 'key3', 'key4', 'nestedlevel1key', 'nestedlevel2key', 'key5', 'key6', 'regularkey']
你可以用递归函数解决这个问题,而不是大量的嵌套循环:
origDict = {
"key1":"string",
"key2":[{"nestedkey":"nestedvalue"}],
"key3":[1,2,3],
"key4":[{"nestedlevel1key":[{"nestedlevel2key":"nestedlevel2value"}]}],
"key5": {},
"key6": {"regularkey": "regularvalue"},
"key7": 15
}
keyList = []
def get_keys(inDict, listOfKeys):
for k, v in inDict.items():
listOfKeys.append(k)
if isinstance(v, list):
for item in v:
if isinstance(item, dict):
get_keys(item, listOfKeys)
elif isinstance(v, dict):
get_keys(v, listOfKeys)
get_keys(origDict, keyList)
print(keyList)
这会打印:
['key1', 'key2', 'nestedkey', 'key3', 'key4', 'nestedlevel1key',
'nestedlevel2key', 'key5', 'key6', 'regularkey', 'key7']```