Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/eclipse/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用列表中的每个值更新dicitonary键_Python - Fatal编程技术网

Python 使用列表中的每个值更新dicitonary键

Python 使用列表中的每个值更新dicitonary键,python,Python,鉴于这一名单: pc = ['OferteConfigurabile_S2D_Rate', 'OferteConfigurabile_S2D_Rate_si_Discount', 'OferteConfigurabile_S2D_SimOnly_si_Discount', 'OferteConfigurabile_S2D_SimOnly', 'OferteConfigurabile_S2D_VMM_Rate'] 这本字典: lst = [] dataModel = { 'cha

鉴于这一名单:

pc = ['OferteConfigurabile_S2D_Rate',
'OferteConfigurabile_S2D_Rate_si_Discount',
'OferteConfigurabile_S2D_SimOnly_si_Discount',
'OferteConfigurabile_S2D_SimOnly',
'OferteConfigurabile_S2D_VMM_Rate']
这本字典:

lst = []
dataModel = {
        'channel': 'RETAIL',
        'searchType': 'MSISDN',
        'searchValue': 727277696,
        'configType': 'RETENTIE',
        'scenarioName_PC': 'OferteConfigurabile_ServiceOnly',
        'retention_option': '360_OFERTE_MOBILE',
        'retention_flow': 'ConfigureazaOferte',
        }
我想让“pc”列表中的每个元素都更新dateModel['scenarioName_pc'],并将生成的字典存储到列表中,但通过创建一个新的dict(使用自定义键和dataModel dictionary作为其值)进行轻微更改

for i in pc:
    dataModel['scenarioName_PC'] = i

    lst.append({f"{dataModel['retention_option']}_{dataModel['retention_flow']}_{i}" : dataModel})

print(lst)

问题是,当我打印列表'scenarioName_PC'键时,总是有迭代列表中的最后一个元素,dataModel字典没有为每个for循环迭代保存值,它只是以某种方式将最后一个值存储在PC列表中

     [

   {

      "360_OFERTE_MOBILE_ConfigureazaOferte_OferteConfigurabile_S2D_Rate":{

         "channel":"RETAIL",

         "searchType":"MSISDN",

         "searchValue":727277696,

         "configType":"RETENTIE",

         "scenarioName_PC":"OferteConfigurabile_S2D_VMM_Rate",

         "retention_option":"360_OFERTE_MOBILE",

         "retention_flow":"ConfigureazaOferte"

      }

   },

   {

      "360_OFERTE_MOBILE_ConfigureazaOferte_OferteConfigurabile_S2D_Rate_si_Discount":{

         "channel":"RETAIL",

         "searchType":"MSISDN",

         "searchValue":727277696,

         "configType":"RETENTIE",

         "scenarioName_PC":"OferteConfigurabile_S2D_VMM_Rate",

         "retention_option":"360_OFERTE_MOBILE",

         "retention_flow":"ConfigureazaOferte"

      }

   },
预期结果是一个带有dataModel dictionary的列表,但scenarioname_PC键的值为每次“i”。 [


追加时,请复制dictionary对象,而不是仅传递dictionary。您传递的是正在修改的dictionary引用

这应该行得通

导入副本
对于个人电脑中的i:
new_dataMode=copy.deepcopy(dataMode)
新的数据模型['scenarioName\u PC']=i
lst.append({f{new_数据模型['retention_option']}{new_数据模型['retention_flow']}}{i}):new_数据模型})
打印(lst)

您可以发布一个所需输出的示例吗?“问题是,当我打印列表“scenarioName\u PC”键时,总是包含迭代列表中的最后一个元素,”-这不是问题..这是dict的工作方式..好的,所以我想让dict中的值为每个for循环更新,使用I,而不仅仅是列表中的最后一个@baldermanm@wundermahn,马上。这回答了你的问题吗?
   {

      "360_OFERTE_MOBILE_ConfigureazaOferte_OferteConfigurabile_S2D_Rate":{

         "channel":"RETAIL",

         "searchType":"MSISDN",

         "searchValue":727277696,

         "configType":"RETENTIE",

         "scenarioName_PC":"OferteConfigurabile_S2D_Rate",

         "retention_option":"360_OFERTE_MOBILE",

         "retention_flow":"ConfigureazaOferte"

      }

   },

   {

      "360_OFERTE_MOBILE_ConfigureazaOferte_OferteConfigurabile_S2D_Rate_si_Discount":{

         "channel":"RETAIL",

         "searchType":"MSISDN",

         "searchValue":727277696,

         "configType":"RETENTIE",

         "scenarioName_PC":"OferteConfigurabile_S2D_Rate_si_Discount",

         "retention_option":"360_OFERTE_MOBILE",

         "retention_flow":"ConfigureazaOferte"

      }

   },