• 我有一个包含UUID个文档的词典,如下所示.
{
  "UUID": [
    "b8f2904b-dafd-4be3-9615-96bac8e16c7f",
    "1240ad39-4815-480f-8cb2-43f802ba8d4e"
  ]
}
  • 和另一本词典作为嵌套词典
{
  "R_Id": 304,
  "ContextKey": "Mr.Dave",
  "ConsolidationInformation": {
    "Input": [
      {
        "Document代码": "BS",
        "ObjectType": "Document",
        "InputContextKey": "Mr.Dave_HDFC.pdf2022-08-010T09:40:06.429358"
      },
      {
        "Document代码": "F16",
        "ObjectType": "Document",
        "InputContextKey": "Mr.Dave_F16.pdf2022-08-010T09:40:06.429358"
      }
    ]
  }
}
  • 我想将UUID by索引添加到['ConsolidationInformation']['Input']中,并在个体Input中添加为DocumentUUID,我如何使用for循环来映射它.我试着在互联网上搜索,但没有找到一个可以满足这个嵌套条件的解决方案.
  • 预期yields
{
  "R_Id": 304,
  "ContextKey": "Mr.Dave",
  "ConsolidationInformation": {
    "Input": [
      {
        "Document代码": "BS",
        "ObjectType": "Document",
        "InputContextKey": "Mr.Dave_HDFC.pdf2022-08-010T09:40:06.429358",
        "DocumentUUID": "b8f2904b-dafd-4be3-9615-96bac8e16c7f"
      },
      {
        "Document代码": "F16",
        "ObjectType": "Document",
        "InputContextKey": "Mr.Dave_F16.pdf2022-08-010T09:40:06.429358",
        "DocumentUUID": "1240ad39-4815-480f-8cb2-43f802ba8d4e"
      }
    ]
  }
}
  • 我try 了下面这样的方法,但结果是
Traceback (most recent call last):
File "<string>", line 26, in <module>
KeyError: 0
  • 代码
uuid = {
  "UUID": [
    "b8f2904b-dafd-4be3-9615-96bac8e16c7f",
    "1240ad39-4815-480f-8cb2-43f802ba8d4e"
  ]
}
document = {
  "R_Id": 304,
  "ContextKey": "Mr.Dave",
  "ConsolidationInformation": {
    "Input": [
      {
        "Document代码": "BS",
        "ObjectType": "Document",
        "InputContextKey": "Mr.Dave_HDFC.pdf2022-08-010T09:40:06.429358"
      },
      {
        "Document代码": "F16",
        "ObjectType": "Document",
        "InputContextKey": "Mr.Dave_F16.pdf2022-08-010T09:40:06.429358"
      }
    ]
  }
}
for i, document in enumerate(document):
    uuid = uuid[i]
    print(f"${uuid} for 1 {document}")

推荐答案

Issues:你的try 做得很好.唯一的问题是必须在正确的嵌套级别访问和添加这些值.

Solution:您可以按如下方式更正您的try :

for i, doc in enumerate(document['ConsolidationInformation']['Input']):
    doc['DocumentUUID'] = uuid['UUID'][i]

Alternatively:可以使用zip函数.你可以了解更多关于这个函数的here.

以下是如何将该函数应用于代码的示例:

for u, doc in zip(uuid['UUID'], document['ConsolidationInformation']['Input']):
    doc['DocumentUUID'] = u

Output:输出如下:

{
   "R_Id":304,
   "ContextKey":"Mr.Dave",
   "ConsolidationInformation":{
      "Input":[
         {
            "DocumentCode":"BS",
            "ObjectType":"Document",
            "InputContextKey":"Mr.Dave_HDFC.pdf2022-08-010T09:40:06.429358",
            "DocumentUUID":"b8f2904b-dafd-4be3-9615-96bac8e16c7f"
         },
         {
            "DocumentCode":"F16",
            "ObjectType":"Document",
            "InputContextKey":"Mr.Dave_F16.pdf2022-08-010T09:40:06.429358",
            "DocumentUUID":"1240ad39-4815-480f-8cb2-43f802ba8d4e"
         }
      ]
   }
}

Python相关问答推荐

Pandas数据帧处理Pandas表中Json内的嵌套列表以获取后续Numpy数组

将C struct 的指针传递给Python中的ioctel

无法导入已安装的模块

如何让pyparparsing匹配1天或2天,但1天和2天失败?

具有症状的分段函数:如何仅针对某些输入值定义函数?

将HTML输出转换为表格中的问题

如何计算列表列行之间的公共元素

追溯(最近最后一次调用):文件C:\Users\Diplom/PycharmProject\Yolo01\Roboflow-4.py,第4行,在模块导入roboflow中

_repr_html_实现自定义__getattr_时未显示

在Python Attrs包中,如何在field_Transformer函数中添加字段?

Pandas - groupby字符串字段并按时间范围 Select

如何记录脚本输出

Stacked bar chart from billrame

在极性中创建条件累积和

Pandas计数符合某些条件的特定列的数量

迭代嵌套字典的值

如何保持服务器发送的事件连接活动?

在单次扫描中创建列表

当条件满足时停止ODE集成?

如何根据rame中的列值分别分组值