【问题标题】:Transform Dictionary with nested elements使用嵌套元素转换字典
【发布时间】:2016-05-21 19:10:22
【问题描述】:

假设我有一系列字典形式的推文,结构如下:

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 
}

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

关于我如何做到这一点的任何建议?谢谢。

【问题讨论】:

    标签: python list dictionary


    【解决方案1】:

    试试这个,

    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?

    【讨论】:

      猜你喜欢
      • 2013-12-07
      • 1970-01-01
      • 2022-06-14
      • 2020-07-12
      • 1970-01-01
      • 2019-12-28
      • 1970-01-01
      • 2016-05-17
      相关资源
      最近更新 更多