简体   繁体   English

迭代字典 python 列表的优雅方式

[英]Elegant way of iterating list of dict python

I have a list of dictionary as below.我有一个字典列表如下。 I need to iterate the list of dictionary and remove the content of the parameters and set as an empty dictionary in sections dictionary.我需要迭代字典列表并删除参数的内容并在部分字典中设置为空字典。

input = [
    {
        "category":"Configuration",
        "sections":[
            {
                "section_name":"Global",
                "parameters":{
                    "name":"first",
                    "age":"second"
                }
            },
            {
                "section_name":"Operator",
                "parameters":{
                    "adrress":"first",
                    "city":"first"
                }
            }
        ]
    },
    {
        "category":"Module",
        "sections":[
            {
                "section_name":"Global",
                "parameters":{
                    "name":"first",
                    "age":"second"
                }
            }
        ]
    }
]

Expected Output:预期 Output:

[
   {
      "category":"Configuration",
      "sections":[
         {
            "section_name":"Global",
            "parameters":{}
         },
         {
            "section_name":"Operator",
            "parameters":{}
         }
      ]
   },
   {
      "category":"Module",
      "sections":[
         {
            "section_name":"Global",
            "parameters":{}
         }
      ]
   }
]

My current code looks like below:我当前的代码如下所示:

category_list = []
for categories in input:
    sections_list = []
    category_name_dict = {"category": categories["category"]}
    for sections_dict in categories["sections"]:
        section = {}
        section["section_name"] = sections_dict['section_name']
        section["parameters"] = {}
        sections_list.append(section)
        category_name_dict["sections"] = sections_list
    category_list.append(category_name_dict)

Is there any elegant and more performant way to do compute this logic.是否有任何优雅且性能更高的方法来计算此逻辑。 Keys such as category, sections, section_name, and parameters are constants. category、sections、section_name 和参数等键是常量。

The easier way is not to rebuild the dictionary without the parameters , just clear it in every section:更简单的方法是不要在没有parameters的情况下重建字典,只需在每个部分中清除它:

for value in values:
    for section in value['sections']:
        section['parameters'] = {}

Code demo

Elegance is in the eye of the beholder, but rather than creating empty lists and dictionaries then filling them why not do it in one go with a list comprehension :优雅在旁观者的眼中,但与其创建空列表和字典然后填充它们,为什么不在一个 go 中使用列表理解

category_list = [
    {
        **category,
        "sections": [
            {
                **section,
                "parameters": {},
            }
            for section in category["sections"]
        ],
    }
    for category in input
]

This is more efficient and (in my opinion) makes it clearer that the intention is to change a single key.这更有效,并且(在我看来)更清楚地表明意图是更改单个键。

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

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