假设我有一系列字典形式的推文,结构如下:
Tweet = {
'created_at': 'Thu Apr 21 16:03:40 +0000 2016',
'text': 'Look at this dog!',
'entities': {'hashtags': ['dog'] },
'favorite_count': 0,
'user': {'screen_name': 'Jake_Jake'},
'retweet_count': 0
}
我想创建一个新字典,以便"主题标签"和"screen_name"键和值不再嵌套:
{'created_at': 'Thu Apr 21 16:03:40 +0000 2016', 'text': 'Look at this dog!', 'favorite_count': 0, 'hashtags': ['dog'], 'retweet_count': 0, 'screen_name': 'Jake_Jake'}
关于我如何实现这一目标的任何建议? 谢谢。
试试这个,
d = dict()
for k, v in iTweet.items():
if isinstance(v, dict):
for k2, v2 in v.items():
d[k2] = v2
else:
d[k] = v
print(d)
{'screen_name': 'Jake_Jake', 'text': 'Look at this dog!', 'created_at': 'Thu Apr 21 16:03:40 +0000 2016', 'hashtags': ['dog'], 'retweet_count': 0, 'favorite_count': 0}
更一般地说,使用递归函数从嵌套字典中提取所有键、值对,
def extract(dict_in, dict_out):
"""extract all the key, value pairs from a nested dict"""
for key, value in dict_in.iteritems():
if isinstance(value, dict): # value itself is dictionary
extract(value, dict_out)
else:
dict_out[key] = value
return dict_out
# Test
dict_out = dict()
extract(iTweet, dict_out)
print(dict_out)
# Output
{'screen_name': 'Jake_Jake', 'text': 'Look at this dog!', 'created_at': 'Thu Apr 21 16:03:40 +0000 2016', 'hashtags': ['dog'], 'retweet_count': 0, 'favorite_count': 0}
请参阅如何获取字典列表和字典的嵌套字典中的所有键和值?。