RError.com

RError.com Logo RError.com Logo

RError.com Navigation

  • 主页

Mobile menu

Close
  • 主页
  • 系统&网络
    • 热门问题
    • 最新问题
    • 标签
  • Ubuntu
    • 热门问题
    • 最新问题
    • 标签
  • 帮助
主页 / 问题 / 1447329
Accepted
Namerek
Namerek
Asked:2022-09-10 15:44:47 +0000 UTC2022-09-10 15:44:47 +0000 UTC 2022-09-10 15:44:47 +0000 UTC

在 Python 中优化 JSON 解析

  • 772

下面的代码功能齐全并执行其任务。
对在不使用第三方库的情况下对其进行优化或实施的可能性感兴趣。
提前感谢您的建设性建议。

编码:


incoming.json

{
  "questions": [
    {
      "key": "what_type_of_business_do_you_operate?", 
      "label": "What type of business do you operate?", 
      "options": [
        {
          "key": "reseller", 
          "value": "Reseller"
        }, 
        {
          "key": "logistics_company", 
          "value": "Logistics company"
        }, 
        {
          "key": "other", 
          "value": "Other"
        }
      ], 
      "type": "CUSTOM", 
      "id": "****************"
    }, 
    {
      "key": "conditional_question_1", 
      "label": "Please choose the truck brand your are interested in", 
      "options": [
        {
          "key": "volvo", 
          "value": "Volvo"
        }, 
        {
          "key": "mercedes-benz_actros", 
          "value": "Mercedes-Benz Actros"
        }, 
        {
          "key": "daf", 
          "value": "DAF"
        }
      ], 
      "type": "CUSTOM", 
      "dependent_conditional_questions": [
        {
          "name": "Please choose age of the vehicle", 
          "input_type": "inline_select", 
          "field_key": "conditional_question_2"
        }, 
        {
          "name": "Quantity you want to purchase", 
          "input_type": "inline_select", 
          "field_key": "conditional_question_3"
        }, 
        {
          "name": "When do you plan to purchase", 
          "input_type": "inline_select", 
          "field_key": "conditional_question_4"
        }
      ], 
      "conditional_questions_choices": [
        {
          "value": "Volvo", 
          "customized_token": "volvo", 
          "next_question_choices": [
            {
              "value": "1-3 years", 
              "customized_token": "1-3_years", 
              "next_question_choices": [
                {
                  "value": "1", 
                  "customized_token": "1", 
                  "next_question_choices": [
                    {
                      "value": "In upcoming days or weeks", 
                      "customized_token": "in_upcoming_days_or_weeks"
                    }, 
                    {
                      "value": "Within 3 months", 
                      "customized_token": "within_3_months"
                    }, 
                    {
                      "value": "This year", 
                      "customized_token": "this_year"
                    }, 
                    {
                      "value": "Next year", 
                      "customized_token": "next_year"
                    }
                  ]
                }, 
                {
                  "value": "2", 
                  "customized_token": "2", 
                  "next_question_choices": [
                    {
                      "value": "In upcoming days or weeks", 
                      "customized_token": "in_upcoming_days_or_weeks"
                    }, 
                    {
                      "value": "Within 3 months", 
                      "customized_token": "within_3_months"
                    }, 
                    {
                      "value": "This year", 
                      "customized_token": "this_year"
                    }, 
                    {
                      "value": "Next year", 
                      "customized_token": "next_year"
                    }
                  ]
                }, 
                {
                  "value": "3-5", 
                  "customized_token": "3-5", 
                  "next_question_choices": [
                    {
                      "value": "In upcoming days or weeks", 
                      "customized_token": "in_upcoming_days_or_weeks"
                    }, 
                    {
                      "value": "Within 3 months", 
                      "customized_token": "within_3_months"
                    }, 
                    {
                      "value": "This year", 
                      "customized_token": "this_year"
                    }, 
                    {
                      "value": "Next year", 
                      "customized_token": "next_year"
                    }
                  ]
                }, 
                {
                  "value": "more than 5", 
                  "customized_token": "more_than_5", 
                  "next_question_choices": [
                    {
                      "value": "Within 3 months", 
                      "customized_token": "within_3_months"
                    }, 
                    {
                      "value": "This year", 
                      "customized_token": "this_year"
                    }, 
                    {
                      "value": "Next year", 
                      "customized_token": "next_year"
                    }
                  ]
                }
              ]
            }, 
            {
              "value": "3-5 years", 
              "customized_token": "3-5_years", 
              "next_question_choices": [
                {
                  "value": "1", 
                  "customized_token": "1", 
                  "next_question_choices": [
                    {
                      "value": "In upcoming days or weeks", 
                      "customized_token": "in_upcoming_days_or_weeks"
                    }, 
                    {
                      "value": "Within 3 months", 
                      "customized_token": "within_3_months"
                    }, 
                    {
                      "value": "This year", 
                      "customized_token": "this_year"
                    }, 
                    {
                      "value": "Next year", 
                      "customized_token": "next_year"
                    }
                  ]
                }, 
                {
                  "value": "2", 
                  "customized_token": "2", 
                  "next_question_choices": [
                    {
                      "value": "In upcoming days or weeks", 
                      "customized_token": "in_upcoming_days_or_weeks"
                    }, 
                    {
                      "value": "Within 3 months", 
                      "customized_token": "within_3_months"
                    }, 
                    {
                      "value": "This year", 
                      "customized_token": "this_year"
                    }, 
                    {
                      "value": "Next year", 
                      "customized_token": "next_year"
                    }
                  ]
                }, 
                {
                  "value": "3-5", 
                  "customized_token": "3-5", 
                  "next_question_choices": [
                    {
                      "value": "In upcoming days or weeks", 
                      "customized_token": "in_upcoming_days_or_weeks"
                    }, 
                    {
                      "value": "Within 3 months", 
                      "customized_token": "within_3_months"
                    }, 
                    {
                      "value": "This year", 
                      "customized_token": "this_year"
                    }, 
                    {
                      "value": "Next year", 
                      "customized_token": "next_year"
                    }
                  ]
                }, 
                {
                  "value": "more than 5", 
                  "customized_token": "more_than_5", 
                  "next_question_choices": [
                    {
                      "value": "In upcoming days or weeks", 
                      "customized_token": "in_upcoming_days_or_weeks"
                    }, 
                    {
                      "value": "Within 3 months", 
                      "customized_token": "within_3_months"
                    }, 
                    {
                      "value": "This year", 
                      "customized_token": "this_year"
                    }, 
                    {
                      "value": "Next year", 
                      "customized_token": "next_year"
                    }
                  ]
                }
              ]
            }
          ]
        }, 
        {
          "value": "Mercedes-Benz Actros", 
          "customized_token": "mercedes-benz_actros", 
          "next_question_choices": [
            {
              "value": "1-3 years", 
              "customized_token": "1-3_years", 
              "next_question_choices": [
                {
                  "value": "1", 
                  "customized_token": "1", 
                  "next_question_choices": [
                    {
                      "value": "In upcoming days or weeks", 
                      "customized_token": "in_upcoming_days_or_weeks"
                    }, 
                    {
                      "value": "Within 3 months", 
                      "customized_token": "within_3_months"
                    }, 
                    {
                      "value": "This year", 
                      "customized_token": "this_year"
                    }, 
                    {
                      "value": "Next year", 
                      "customized_token": "next_year"
                    }
                  ]
                }, 
                {
                  "value": "2", 
                  "customized_token": "2", 
                  "next_question_choices": [
                    {
                      "value": "In upcoming days or weeks", 
                      "customized_token": "in_upcoming_days_or_weeks"
                    }, 
                    {
                      "value": "Within 3 months", 
                      "customized_token": "within_3_months"
                    }, 
                    {
                      "value": "This year", 
                      "customized_token": "this_year"
                    }, 
                    {
                      "value": "Next year", 
                      "customized_token": "next_year"
                    }
                  ]
                }, 
                {
                  "value": "3-5", 
                  "customized_token": "3-5", 
                  "next_question_choices": [
                    {
                      "value": "In upcoming days or weeks", 
                      "customized_token": "in_upcoming_days_or_weeks"
                    }, 
                    {
                      "value": "Within 3 months", 
                      "customized_token": "within_3_months"
                    }, 
                    {
                      "value": "This year", 
                      "customized_token": "this_year"
                    }, 
                    {
                      "value": "Next year", 
                      "customized_token": "next_year"
                    }
                  ]
                }, 
                {
                  "value": "more than 5", 
                  "customized_token": "more_than_5", 
                  "next_question_choices": [
                    {
                      "value": "In upcoming days or weeks", 
                      "customized_token": "in_upcoming_days_or_weeks"
                    }, 
                    {
                      "value": "Within 3 months", 
                      "customized_token": "within_3_months"
                    }, 
                    {
                      "value": "This year", 
                      "customized_token": "this_year"
                    }, 
                    {
                      "value": "Next year", 
                      "customized_token": "next_year"
                    }
                  ]
                }
              ]
            }, 
            {
              "value": "3-5 years", 
              "customized_token": "3-5_years", 
              "next_question_choices": [
                {
                  "value": "1", 
                  "customized_token": "1", 
                  "next_question_choices": [
                    {
                      "value": "In upcoming days or weeks", 
                      "customized_token": "in_upcoming_days_or_weeks"
                    }, 
                    {
                      "value": "Within 3 months", 
                      "customized_token": "within_3_months"
                    }, 
                    {
                      "value": "This year", 
                      "customized_token": "this_year"
                    }, 
                    {
                      "value": "Next year", 
                      "customized_token": "next_year"
                    }
                  ]
                }, 
                {
                  "value": "2", 
                  "customized_token": "2", 
                  "next_question_choices": [
                    {
                      "value": "In upcoming days or weeks", 
                      "customized_token": "in_upcoming_days_or_weeks"
                    }, 
                    {
                      "value": "Within 3 months", 
                      "customized_token": "within_3_months"
                    }, 
                    {
                      "value": "This year", 
                      "customized_token": "this_year"
                    }, 
                    {
                      "value": "Next year", 
                      "customized_token": "next_year"
                    }
                  ]
                }, 
                {
                  "value": "3-5", 
                  "customized_token": "3-5", 
                  "next_question_choices": [
                    {
                      "value": "In upcoming days or weeks", 
                      "customized_token": "in_upcoming_days_or_weeks"
                    }, 
                    {
                      "value": "Within 3 months", 
                      "customized_token": "within_3_months"
                    }, 
                    {
                      "value": "This year", 
                      "customized_token": "this_year"
                    }, 
                    {
                      "value": "Next year", 
                      "customized_token": "next_year"
                    }
                  ]
                }, 
                {
                  "value": "more than 5", 
                  "customized_token": "more_than_5", 
                  "next_question_choices": [
                    {
                      "value": "In upcoming days or weeks", 
                      "customized_token": "in_upcoming_days_or_weeks"
                    }, 
                    {
                      "value": "Within 3 months", 
                      "customized_token": "within_3_months"
                    }, 
                    {
                      "value": "This year", 
                      "customized_token": "this_year"
                    }, 
                    {
                      "value": "Next year", 
                      "customized_token": "next_year"
                    }
                  ]
                }
              ]
            }
          ]
        }, 
        {
          "value": "DAF", 
          "customized_token": "daf", 
          "next_question_choices": [
            {
              "value": "3-5 years", 
              "customized_token": "3-5_years", 
              "next_question_choices": [
                {
                  "value": "1", 
                  "customized_token": "1", 
                  "next_question_choices": [
                    {
                      "value": "In upcoming days or weeks", 
                      "customized_token": "in_upcoming_days_or_weeks"
                    }, 
                    {
                      "value": "Within 3 months", 
                      "customized_token": "within_3_months"
                    }, 
                    {
                      "value": "This year", 
                      "customized_token": "this_year"
                    }, 
                    {
                      "value": "Next year", 
                      "customized_token": "next_year"
                    }
                  ]
                }, 
                {
                  "value": "2", 
                  "customized_token": "2", 
                  "next_question_choices": [
                    {
                      "value": "In upcoming days or weeks", 
                      "customized_token": "in_upcoming_days_or_weeks"
                    }, 
                    {
                      "value": "Within 3 months", 
                      "customized_token": "within_3_months"
                    }, 
                    {
                      "value": "This year", 
                      "customized_token": "this_year"
                    }, 
                    {
                      "value": "Next year", 
                      "customized_token": "next_year"
                    }
                  ]
                }, 
                {
                  "value": "3-5", 
                  "customized_token": "3-5", 
                  "next_question_choices": [
                    {
                      "value": "In upcoming days or weeks", 
                      "customized_token": "in_upcoming_days_or_weeks"
                    }, 
                    {
                      "value": "Within 3 months", 
                      "customized_token": "within_3_months"
                    }, 
                    {
                      "value": "This year", 
                      "customized_token": "this_year"
                    }, 
                    {
                      "value": "Next year", 
                      "customized_token": "next_year"
                    }
                  ]
                }, 
                {
                  "value": "more than 5", 
                  "customized_token": "more_than_5", 
                  "next_question_choices": [
                    {
                      "value": "In upcoming days or weeks", 
                      "customized_token": "in_upcoming_days_or_weeks"
                    }, 
                    {
                      "value": "Within 3 months", 
                      "customized_token": "within_3_months"
                    }, 
                    {
                      "value": "This year", 
                      "customized_token": "this_year"
                    }, 
                    {
                      "value": "Next year", 
                      "customized_token": "next_year"
                    }
                  ]
                }
              ]
            }
          ]
        }
      ], 
      "conditional_questions_group_id": "1781381718904816", 
      "id": "****************"
    }, 
    {
      "key": "company_name", 
      "label": "Company name", 
      "type": "COMPANY_NAME", 
      "id": "****************"
    }, 
    {
      "key": "full_name", 
      "label": "Full name", 
      "type": "FULL_NAME", 
      "id": "****************"
    }, 
    {
      "key": "phone_number", 
      "label": "Phone number", 
      "type": "PHONE", 
      "id": "****************"
    }, 
    {
      "key": "email", 
      "label": "Email", 
      "type": "EMAIL", 
      "id": "****************"
    }
  ], 
  "locale": "en_US", 
  "name": "Resellers - Retail lead form test 220908 multiple sales people", 
  "created_time": "2022-09-08T11:32:03+0000", 
  "context_card": {
    "title": "Testing multiple sales people", 
    "content": [
      "aekejskal;fjkldsfjdklsfj"
    ], 
    "style": "PARAGRAPH_STYLE", 
    "id": "****************"
  }, 
  "id": "****************"
}

处理程序:
pip install jmespath

import jmespath as jp
import json

data: dict = json.load(
    open('incoming.json', 'r', encoding='utf-8'),
)

form_questions = dict()

for item in jp.search(
        'questions[?dependent_conditional_questions!=null]',
        data
):
    item: dict
    questions = {
        item.get('key'): {
            'label': item.get('label'),
            'type': 'CONDITIONAL'
        }
    }
    for elem in jp.search(
            'dependent_conditional_questions[*].[field_key,{label:name}]',
            item
    ):
        elem[1]['type'] = 'CONDITIONAL'
        questions.update(
            (elem,)
        )

    for i, key in enumerate(questions):
        pat = f'conditional_questions_choices' + ('[0].next_question_choices' * i) + '[*].[customized_token,value]'
        asd = questions[key]['values'] = dict(jp.search(pat, item))

    form_questions.update(
        questions
    )

for item in jp.search(
        "questions[?dependent_conditional_questions==null && type=='CUSTOM']",
        data
):
    item: dict

    form_questions.update(
        {
            item.get('key'):
                {
                    'label': item.get('label'),
                    'values': {
                        elem.get('key'): elem.get('value')
                        for elem in item.get('options', [])
                    },
                    'type': 'CUSTOM'
                }
        }
    )

for item in jp.search(
        "questions[?type=='COMPANY_NAME' || type=='PHONE' || type=='FULL_NAME' || type=='EMAIL']",
        data
):
    item: dict
    form_questions.update(
        {
            item.get('key'): {
                'label': item.get('label'),
                'type': item.get('type'),
                'values': dict()
            }
        }
    )

结果:

{
  "conditional_question_1": {
    "label": "Please choose the truck brand your are interested in", 
    "type": "CONDITIONAL", 
    "values": {
      "volvo": "Volvo", 
      "mercedes-benz_actros": "Mercedes-Benz Actros", 
      "daf": "DAF"
    }
  }, 
  "conditional_question_2": {
    "label": "Please choose age of the vehicle", 
    "type": "CONDITIONAL", 
    "values": {
      "1-3_years": "1-3 years", 
      "3-5_years": "3-5 years"
    }
  }, 
  "conditional_question_3": {
    "label": "Quantity you want to purchase", 
    "type": "CONDITIONAL", 
    "values": {
      "1": "1", 
      "2": "2", 
      "3-5": "3-5", 
      "more_than_5": "more than 5"
    }
  }, 
  "conditional_question_4": {
    "label": "When do you plan to purchase", 
    "type": "CONDITIONAL", 
    "values": {
      "in_upcoming_days_or_weeks": "In upcoming days or weeks", 
      "within_3_months": "Within 3 months", 
      "this_year": "This year", 
      "next_year": "Next year"
    }
  }, 
  "what_type_of_business_do_you_operate?": {
    "label": "What type of business do you operate?", 
    "values": {
      "reseller": "Reseller", 
      "logistics_company": "Logistics company", 
      "other": "Other"
    }, 
    "type": "CUSTOM"
  }, 
  "company_name": {
    "label": "Company name", 
    "type": "COMPANY_NAME", 
    "values": {}
  }, 
  "full_name": {
    "label": "Full name", 
    "type": "FULL_NAME", 
    "values": {}
  }, 
  "phone_number": {
    "label": "Phone number", 
    "type": "PHONE", 
    "values": {}
  }, 
  "email": {
    "label": "Email", 
    "type": "EMAIL", 
    "values": {}
  }
}
python
  • 0 0 个回答
  • 0 Views

0 个回答

  • Voted
  1. Best Answer
    Сергей Кох
    2022-09-15T00:45:38Z2022-09-15T00:45:38Z

    使用标准库的选项。查看 json 和所需的结果,您可以看到字典几乎是统一构建的,因此:

    1. 它建议使用其 object_pairs_hook 函数进行递归 json 解码。OrderedDict 示例:
    with open("incoming.json", 'r') as file:
        ordered_dict = json.load(file, object_pairs_hook=OrderedDict)
    for d in ordered_dict['questions']:
        print(d)
    
    OrderedDict([('key', 'what_type_of_business_do_you_operate?'), ('label', 'What type of business do you operate?'), ('options', [OrderedDict([('key', 'reseller'), ('value', 'Reseller')]), OrderedDict([('key', 'logistics_company'), ('value', 'Logistics company')]), OrderedDict([('key', 'other'), ('value', 'Other')])]), ('type', 'CUSTOM'), ('id', '****************')])
    OrderedDict([('key', 'conditional_question_1'), ('label', 'Please choose the truck brand your are interested in'), ('options', [OrderedDict([('key', 'volvo'), ('value', 'Volvo')]), OrderedDict([('key', 'mercedes-benz_actros'), ('value', 'Mercedes-Benz Actros')]), OrderedDict([('key', 'daf'), ('value', 'DAF')])]), ('type', 'CUSTOM'), ('dependent_conditional_questions', [OrderedDict([('name', 'Please choose age of the vehicle'), ('input_type', 'inline_select'), ('field_key', 'conditional_question_2')]), OrderedDict([('name', 'Quantity you want to purchase'), ('input_type', 'inline_select'), ('field_key', 'conditional_question_3')]), OrderedDict([('name', 'When do you plan to purchase'), ('input_type', 'inline_select'), ('field_key', 'conditional_question_4')])]), ('conditional_questions_choices', [OrderedDict([('value', 'Volvo'), ('customized_token', 'volvo'), ('next_question_choices', [OrderedDict([('value', '1-3 years'), ('customized_token', '1-3_years'), ('next_question_choices', [OrderedDict([('value', '1'), ('customized_token', '1'), ('next_question_choices', [OrderedDict([('value', 'In upcoming days or weeks'), ('customized_token', 'in_upcoming_days_or_weeks')]), OrderedDict([('value', 'Within 3 months'), ('customized_token', 'within_3_months')]), OrderedDict([('value', 'This year'), ('customized_token', 'this_year')]), OrderedDict([('value', 'Next year'), ('customized_token', 'next_year')])])]), OrderedDict([('value', '2'), ('customized_token', '2'), ('next_question_choices', [OrderedDict([('value', 'In upcoming days or weeks'), ('customized_token', 'in_upcoming_days_or_weeks')]), OrderedDict([('value', 'Within 3 months'), ('customized_token', 'within_3_months')]), OrderedDict([('value', 'This year'), ('customized_token', 'this_year')]), OrderedDict([('value', 'Next year'), ('customized_token', 'next_year')])])]), OrderedDict([('value', '3-5'), ('customized_token', '3-5'), ('next_question_choices', [OrderedDict([('value', 'In upcoming days or weeks'), ('customized_token', 'in_upcoming_days_or_weeks')]), OrderedDict([('value', 'Within 3 months'), ('customized_token', 'within_3_months')]), OrderedDict([('value', 'This year'), ('customized_token', 'this_year')]), OrderedDict([('value', 'Next year'), ('customized_token', 'next_year')])])]), OrderedDict([('value', 'more than 5'), ('customized_token', 'more_than_5'), ('next_question_choices', [OrderedDict([('value', 'Within 3 months'), ('customized_token', 'within_3_months')]), OrderedDict([('value', 'This year'), ('customized_token', 'this_year')]), OrderedDict([('value', 'Next year'), ('customized_token', 'next_year')])])])])]), OrderedDict([('value', '3-5 years'), ('customized_token', '3-5_years'), ('next_question_choices', [OrderedDict([('value', '1'), ('customized_token', '1'), ('next_question_choices', [OrderedDict([('value', 'In upcoming days or weeks'), ('customized_token', 'in_upcoming_days_or_weeks')]), OrderedDict([('value', 'Within 3 months'), ('customized_token', 'within_3_months')]), OrderedDict([('value', 'This year'), ('customized_token', 'this_year')]), OrderedDict([('value', 'Next year'), ('customized_token', 'next_year')])])]), OrderedDict([('value', '2'), ('customized_token', '2'), ('next_question_choices', [OrderedDict([('value', 'In upcoming days or weeks'), ('customized_token', 'in_upcoming_days_or_weeks')]), OrderedDict([('value', 'Within 3 months'), ('customized_token', 'within_3_months')]), OrderedDict([('value', 'This year'), ('customized_token', 'this_year')]), OrderedDict([('value', 'Next year'), ('customized_token', 'next_year')])])]), OrderedDict([('value', '3-5'), ('customized_token', '3-5'), ('next_question_choices', [OrderedDict([('value', 'In upcoming days or weeks'), ('customized_token', 'in_upcoming_days_or_weeks')]), OrderedDict([('value', 'Within 3 months'), ('customized_token', 'within_3_months')]), OrderedDict([('value', 'This year'), ('customized_token', 'this_year')]), OrderedDict([('value', 'Next year'), ('customized_token', 'next_year')])])]), OrderedDict([('value', 'more than 5'), ('customized_token', 'more_than_5'), ('next_question_choices', [OrderedDict([('value', 'In upcoming days or weeks'), ('customized_token', 'in_upcoming_days_or_weeks')]), OrderedDict([('value', 'Within 3 months'), ('customized_token', 'within_3_months')]), OrderedDict([('value', 'This year'), ('customized_token', 'this_year')]), OrderedDict([('value', 'Next year'), ('customized_token', 'next_year')])])])])])])]), OrderedDict([('value', 'Mercedes-Benz Actros'), ('customized_token', 'mercedes-benz_actros'), ('next_question_choices', [OrderedDict([('value', '1-3 years'), ('customized_token', '1-3_years'), ('next_question_choices', [OrderedDict([('value', '1'), ('customized_token', '1'), ('next_question_choices', [OrderedDict([('value', 'In upcoming days or weeks'), ('customized_token', 'in_upcoming_days_or_weeks')]), OrderedDict([('value', 'Within 3 months'), ('customized_token', 'within_3_months')]), OrderedDict([('value', 'This year'), ('customized_token', 'this_year')]), OrderedDict([('value', 'Next year'), ('customized_token', 'next_year')])])]), OrderedDict([('value', '2'), ('customized_token', '2'), ('next_question_choices', [OrderedDict([('value', 'In upcoming days or weeks'), ('customized_token', 'in_upcoming_days_or_weeks')]), OrderedDict([('value', 'Within 3 months'), ('customized_token', 'within_3_months')]), OrderedDict([('value', 'This year'), ('customized_token', 'this_year')]), OrderedDict([('value', 'Next year'), ('customized_token', 'next_year')])])]), OrderedDict([('value', '3-5'), ('customized_token', '3-5'), ('next_question_choices', [OrderedDict([('value', 'In upcoming days or weeks'), ('customized_token', 'in_upcoming_days_or_weeks')]), OrderedDict([('value', 'Within 3 months'), ('customized_token', 'within_3_months')]), OrderedDict([('value', 'This year'), ('customized_token', 'this_year')]), OrderedDict([('value', 'Next year'), ('customized_token', 'next_year')])])]), OrderedDict([('value', 'more than 5'), ('customized_token', 'more_than_5'), ('next_question_choices', [OrderedDict([('value', 'In upcoming days or weeks'), ('customized_token', 'in_upcoming_days_or_weeks')]), OrderedDict([('value', 'Within 3 months'), ('customized_token', 'within_3_months')]), OrderedDict([('value', 'This year'), ('customized_token', 'this_year')]), OrderedDict([('value', 'Next year'), ('customized_token', 'next_year')])])])])]), OrderedDict([('value', '3-5 years'), ('customized_token', '3-5_years'), ('next_question_choices', [OrderedDict([('value', '1'), ('customized_token', '1'), ('next_question_choices', [OrderedDict([('value', 'In upcoming days or weeks'), ('customized_token', 'in_upcoming_days_or_weeks')]), OrderedDict([('value', 'Within 3 months'), ('customized_token', 'within_3_months')]), OrderedDict([('value', 'This year'), ('customized_token', 'this_year')]), OrderedDict([('value', 'Next year'), ('customized_token', 'next_year')])])]), OrderedDict([('value', '2'), ('customized_token', '2'), ('next_question_choices', [OrderedDict([('value', 'In upcoming days or weeks'), ('customized_token', 'in_upcoming_days_or_weeks')]), OrderedDict([('value', 'Within 3 months'), ('customized_token', 'within_3_months')]), OrderedDict([('value', 'This year'), ('customized_token', 'this_year')]), OrderedDict([('value', 'Next year'), ('customized_token', 'next_year')])])]), OrderedDict([('value', '3-5'), ('customized_token', '3-5'), ('next_question_choices', [OrderedDict([('value', 'In upcoming days or weeks'), ('customized_token', 'in_upcoming_days_or_weeks')]), OrderedDict([('value', 'Within 3 months'), ('customized_token', 'within_3_months')]), OrderedDict([('value', 'This year'), ('customized_token', 'this_year')]), OrderedDict([('value', 'Next year'), ('customized_token', 'next_year')])])]), OrderedDict([('value', 'more than 5'), ('customized_token', 'more_than_5'), ('next_question_choices', [OrderedDict([('value', 'In upcoming days or weeks'), ('customized_token', 'in_upcoming_days_or_weeks')]), OrderedDict([('value', 'Within 3 months'), ('customized_token', 'within_3_months')]), OrderedDict([('value', 'This year'), ('customized_token', 'this_year')]), OrderedDict([('value', 'Next year'), ('customized_token', 'next_year')])])])])])])]), OrderedDict([('value', 'DAF'), ('customized_token', 'daf'), ('next_question_choices', [OrderedDict([('value', '3-5 years'), ('customized_token', '3-5_years'), ('next_question_choices', [OrderedDict([('value', '1'), ('customized_token', '1'), ('next_question_choices', [OrderedDict([('value', 'In upcoming days or weeks'), ('customized_token', 'in_upcoming_days_or_weeks')]), OrderedDict([('value', 'Within 3 months'), ('customized_token', 'within_3_months')]), OrderedDict([('value', 'This year'), ('customized_token', 'this_year')]), OrderedDict([('value', 'Next year'), ('customized_token', 'next_year')])])]), OrderedDict([('value', '2'), ('customized_token', '2'), ('next_question_choices', [OrderedDict([('value', 'In upcoming days or weeks'), ('customized_token', 'in_upcoming_days_or_weeks')]), OrderedDict([('value', 'Within 3 months'), ('customized_token', 'within_3_months')]), OrderedDict([('value', 'This year'), ('customized_token', 'this_year')]), OrderedDict([('value', 'Next year'), ('customized_token', 'next_year')])])]), OrderedDict([('value', '3-5'), ('customized_token', '3-5'), ('next_question_choices', [OrderedDict([('value', 'In upcoming days or weeks'), ('customized_token', 'in_upcoming_days_or_weeks')]), OrderedDict([('value', 'Within 3 months'), ('customized_token', 'within_3_months')]), OrderedDict([('value', 'This year'), ('customized_token', 'this_year')]), OrderedDict([('value', 'Next year'), ('customized_token', 'next_year')])])]), OrderedDict([('value', 'more than 5'), ('customized_token', 'more_than_5'), ('next_question_choices', [OrderedDict([('value', 'In upcoming days or weeks'), ('customized_token', 'in_upcoming_days_or_weeks')]), OrderedDict([('value', 'Within 3 months'), ('customized_token', 'within_3_months')]), OrderedDict([('value', 'This year'), ('customized_token', 'this_year')]), OrderedDict([('value', 'Next year'), ('customized_token', 'next_year')])])])])])])])]), ('conditional_questions_group_id', '1781381718904816'), ('id', '****************')])
    OrderedDict([('key', 'company_name'), ('label', 'Company name'), ('type', 'COMPANY_NAME'), ('id', '****************')])
    OrderedDict([('key', 'full_name'), ('label', 'Full name'), ('type', 'FULL_NAME'), ('id', '****************')])
    OrderedDict([('key', 'phone_number'), ('label', 'Phone number'), ('type', 'PHONE'), ('id', '****************')])
    OrderedDict([('key', 'email'), ('label', 'Email'), ('type', 'EMAIL'), ('id', '****************')])
    

    这表明该功能适用​​于所有,包括嵌套字典。

    1. 看起来 OrderedDict 会派上用场,因为我们需要删除键并将值收集到新字典中。从必要的元素组装一个新的字典(没有递归的例子):
    new_dict = OrderedDict({d['key']: OrderedDict(islice(d.items(), 1, None)) for d in ordered_dict['questions']})
    OrderedDict([('what_type_of_business_do_you_operate?', OrderedDict([('label', 'What type of business do you operate?'), ('options',....
    

    видно, что нам нужно удалить из исходного словаря первый элемент и вставить его значение как ключ в новый словарь, а остаточный хвост (без первого элемента) - в значение. Может пойдет popitem(last = False), но в однострочнике у меня не получилось. Если запустить подобный код рекурсивно, выбирая только нужные элементы, то должен получиться нужный словарь. Может даже так:

    new_dict = {d['key']: OrderedDict(islice(d.items(), 1, None)) for d in ordered_dict['questions']}
    {'what_type_of_business_do_you_operate?': OrderedDict([('label', 'What type of business do you operate?'), ('options'...
    

    сразу получается словарь.

    1. Собрать полученные словари в один.

    2. Преобразовать OrderedDict в простой словарь.

    Конечно это только набросок, но буду рад если он поможет вам.

    • 1

相关问题

  • 是否可以以某种方式自定义 QTabWidget?

  • telebot.anihelper.ApiException 错误

  • Python。检查一个数字是否是 3 的幂。输出 无

  • 解析多个响应

  • 交换两个数组的元素,以便它们的新内容也反转

Sidebar

Stats

  • 问题 10021
  • Answers 30001
  • 最佳答案 8000
  • 用户 6900
  • 常问
  • 回答
  • Marko Smith

    我看不懂措辞

    • 1 个回答
  • Marko Smith

    请求的模块“del”不提供名为“default”的导出

    • 3 个回答
  • Marko Smith

    "!+tab" 在 HTML 的 vs 代码中不起作用

    • 5 个回答
  • Marko Smith

    我正在尝试解决“猜词”的问题。Python

    • 2 个回答
  • Marko Smith

    可以使用哪些命令将当前指针移动到指定的提交而不更改工作目录中的文件?

    • 1 个回答
  • Marko Smith

    Python解析野莓

    • 1 个回答
  • Marko Smith

    问题:“警告:检查最新版本的 pip 时出错。”

    • 2 个回答
  • Marko Smith

    帮助编写一个用值填充变量的循环。解决这个问题

    • 2 个回答
  • Marko Smith

    尽管依赖数组为空,但在渲染上调用了 2 次 useEffect

    • 2 个回答
  • Marko Smith

    数据不通过 Telegram.WebApp.sendData 发送

    • 1 个回答
  • Martin Hope
    Alexandr_TT 2020年新年大赛! 2020-12-20 18:20:21 +0000 UTC
  • Martin Hope
    Alexandr_TT 圣诞树动画 2020-12-23 00:38:08 +0000 UTC
  • Martin Hope
    Air 究竟是什么标识了网站访问者? 2020-11-03 15:49:20 +0000 UTC
  • Martin Hope
    Qwertiy 号码显示 9223372036854775807 2020-07-11 18:16:49 +0000 UTC
  • Martin Hope
    user216109 如何为黑客设下陷阱,或充分击退攻击? 2020-05-10 02:22:52 +0000 UTC
  • Martin Hope
    Qwertiy 并变成3个无穷大 2020-11-06 07:15:57 +0000 UTC
  • Martin Hope
    koks_rs 什么是样板代码? 2020-10-27 15:43:19 +0000 UTC
  • Martin Hope
    Sirop4ik 向 git 提交发布的正确方法是什么? 2020-10-05 00:02:00 +0000 UTC
  • Martin Hope
    faoxis 为什么在这么多示例中函数都称为 foo? 2020-08-15 04:42:49 +0000 UTC
  • Martin Hope
    Pavel Mayorov 如何从事件或回调函数中返回值?或者至少等他们完成。 2020-08-11 16:49:28 +0000 UTC

热门标签

javascript python java php c# c++ html android jquery mysql

Explore

  • 主页
  • 问题
    • 热门问题
    • 最新问题
  • 标签
  • 帮助

Footer

RError.com

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

帮助

© 2023 RError.com All Rights Reserve   沪ICP备12040472号-5