【问题标题】:add key value in nested dictionary在嵌套字典中添加键值
【发布时间】:2020-07-06 04:31:38
【问题描述】:
datainput = {'thissong-fav-user:type1-chan-44-John': [{'Song': 'Rock',
                                                       'Type': 'Hard',
                                                       'Price': '10'}],
             'thissong-fav-user:type1-chan-45-kelly-md': [{'Song': 'Rock',
                                                           'Type': 'Soft',
                                                           'Price': '5'}]}

需要输出:

{'thissong-fav-user:type1-chan-44-John': [{key:'Song',Value:'Rock'},
                                          {key:'Type', Value:'Hard'},
                                          {Key: 'Price', Value:'10'}],
 'thissong-fav-user:type1-chan-45-kelly-md': [{key:'Song',Value:'Rock'},
                                              {key:'Type', Value:'Soft'},
                                              {Key: 'Price', Value:'5'}]}

我从下面开始,这给了我一个内部嵌套模式,不确定如何获得所需的输出。

temps = [{'Key': key, 'Value': value} for (key, value) in datainput.items()] 

【问题讨论】:

    标签: python list dictionary nested


    【解决方案1】:

    方法如下:

    datainput = {'thissong-fav-user:type1-chan-44-John': [{'Song': 'Rock',
                                                           'Type': 'Hard',
                                                           'Price': '10'}],
                 'thissong-fav-user:type1-chan-45-kelly-md': [{'Song': 'Rock',
                                                               'Type': 'Soft',
                                                               'Price': '5'}]}
    temps = {k:[{'Key':a, 'Value':b}
                for a,b in v[0].items()]
             for k,v in datainput.items()}
    
    print(datainput)
    

    输出:

    {'thissong-fav-user:type1-chan-44-John': [{'Key': 'Song', 'Value': 'Rock'},
                                              {'Key': 'Type', 'Value': 'Hard'},
                                              {'Key': 'Price', 'Value': '10'}],
     'thissong-fav-user:type1-chan-45-kelly-md': [{'Key': 'Song', 'Value': 'Rock'},
                                                  {'Key': 'Type', 'Value': 'Soft'},
                                                  {'Key': 'Price', 'Value': '5'}]}
    

    【讨论】:

      【解决方案2】:

      我相信获取输入的方式很好,但为了获得所需的输出,您必须首先获取输入,然后是键值对,最后是迭代。

      datainput = {'thissong-fav-user:type1-chan-44-John': [{'Song': 'Rock',
                                                             'Type': 'Hard',
                                                             'Price': '10'}],
                   'thissong-fav-user:type1-chan-45-kelly-md': [{'Song': 'Rock',
                                                                 'Type': 'Soft',
                                                                 'Price': '5'}]}
      datainput = {k:[{'Key':a, 'Value':b} for a,b in v[0].items()] for k,v in datainput.items()}
      
      print(datainput)
      

      您很可能会以这种方式获得所需的输出。

      【讨论】:

        猜你喜欢
        • 2018-08-17
        • 2020-08-17
        • 2018-04-25
        • 2019-12-17
        • 1970-01-01
        • 1970-01-01
        • 2021-11-23
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多