简体   繁体   English

将复杂的字典列表转换为字典

[英]Converting a complex dictionary list to a dictionary

I would like to convert this dictionary list below into a dictionary我想将下面的字典列表转换成字典

data=[
    {
        'code':'matata',
        'commandes':[
            {
                'date':'12-10-22',
                'content':[
                    {
                        'article':'Article1',
                        'designation':'Designe1',
                        'quantity':5
                    }
                ]
            }
        ]
    }
]

And I would like to have this result, just the starting brackets that we change to {..}我想要这个结果,只是我们更改为 {..} 的起始括号

data={
{
    'code':'matata',
    'commandes':[
        {
            'date':'12-10-22',
            'content':[
                {
                    'article':'Article1',
                    'designation':'Designe1',
                    'quantity':5
                }
            ]
        }
    ]
  }
}

Rather, You can use the 0th index of list and return dictionary.相反,您可以使用列表的第 0 个索引并返回字典。

data = data[0]
print(data)

#output
data ={
    'code':'matata',
    'commandes':[
        {
            'date':'12-10-22',
            'content':[
                {
                    'article':'Article1',
                    'designation':'Designe1',
                    'quantity':5
                }
            ]
        }
    ]
  }

(UPDATE 1)If you have multiple proucts: (更新 1)如果您有多个产品:

for product in data:
   print(product)

(UPDATE 2) To print comma: (更新 2)要打印逗号:

for product in data:
   print(product, end =",")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM