返回包含n级嵌套字典的所有键路径的列表



我想创建一个函数,从下面的嵌套字典返回一个列表列表,其中大列表中的每个列表都是嵌套字典中的有序键路径。

这个字典是一个假的Twitter用户对象。

{'username': 'fakeusername',
'location': 'Chicago, IL',
'description': 'fakedescription',
'name': 'fakename',
'url': '',
'created_at': '2018-01-06T16:02:07.000Z',
'verified': False,
'profile_image_url': 'notreal',
'entities': {'description': {'hashtags': [{'start': 43,
'end': 56,
'tag': 'blahblahblah'},
{'start': 59, 'end': 65, 'tag': 'blahblah'},
{'start': 125, 'end': 138, 'tag': 'blah'}]}},
'protected': False,
'id': '000000000000',
'public_metrics': {'followers_count': 31779,
'following_count': 31116,
'tweet_count': 214997,
'listed_count': 7},
'pinned_tweet_id': None}

输出应该像这样:

[
["username"],
["location"],
["description"],
["name"],
["url"],
["created_at"],
["verified"],
["profile_image_url"],
["entities", "description","hashtags"],
["entities", "description","hashtags"],
["protected"],
["id"],
["public_metrics","followers_count"],
["public_metrics","following_count"],
["public_metrics","tweet_count"],
["public_metrics","listed_count"],
["pinned_tweet_id"]
]

您可以使用递归生成器函数:

data = {'username': 'fakeusername', 'location': 'Chicago, IL', 'description': 'fakedescription', 'name': 'fakename', 'url': '', 'created_at': '2018-01-06T16:02:07.000Z', 'verified': False, 'profile_image_url': 'notreal', 'entities': {'description': {'hashtags': [{'start': 43, 'end': 56, 'tag': 'blahblahblah'}, {'start': 59, 'end': 65, 'tag': 'blahblah'}, {'start': 125, 'end': 138, 'tag': 'blah'}]}}, 'protected': False, 'id': '000000000000', 'public_metrics': {'followers_count': 31779, 'following_count': 31116, 'tweet_count': 214997, 'listed_count': 7}, 'pinned_tweet_id': None}
def get_paths(d, p = []):
if not isinstance(d, dict):
yield p
else:
yield from [j for a, b in d.items() for j in get_paths(b, p+[a])]
print(list(get_paths(data)))

输出:

[['username'], ['location'], ['description'], ['name'], ['url'], ['created_at'], ['verified'], ['profile_image_url'], ['entities', 'description', 'hashtags'], ['protected'], ['id'], ['public_metrics', 'followers_count'], ['public_metrics', 'following_count'], ['public_metrics', 'tweet_count'], ['public_metrics', 'listed_count'], ['pinned_tweet_id']]

最新更新