使用嵌套元素转换字典

时间:2016-05-21 19:10:23

标签: python list dictionary

假设我在以下结构中以字典的形式发布了一系列推文:

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 
}

我想创建一个新词典,以便“#tatatata”'和' 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'}

关于如何实现这一目标的任何建议?谢谢。

1 个答案:

答案 0 :(得分:2)

试试这个,

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}

请参阅how to get all keys&values in nested dict of list-of-dicts and dicts?