简体   繁体   English

在嵌套结构中编辑 python 字典键

[英]Editing python dictionary keys in nested structures

I have the following structure to edit, and I am not sure how to do it.我有以下结构要编辑,但我不知道该怎么做。 Eg I need to rename 'Email' into 'emails' and have only email address in the value.例如,我需要将“电子邮件”重命名为“电子邮件”,并且值中只有电子邮件地址。

{'input_text': 'some text',
 'lang': 'eng',
 'response': {'Certification': [{'CertificationName': 'some_name'}],
  'Email': [{'Address': 'someaddress@gmail.com', 'Label': 'personal'}],
  'ExecutiveSummary': ' text',
  'FamilyName': 'Smith',
  'FormattedName': 'John',
  'GivenName': 'John'}
}

This is what I would like这是我想要的

{'input_text': 'some text',
 'lang': 'eng',
 'response': {'Certification': [{'CertificationName': 'some_name'}],
  'emails': ['someaddress@gmail.com'],
  'ExecutiveSummary': ' text',
  'FamilyName': 'Smith',
  'FormattedName': 'John',
  'GivenName': 'John'}

I have had a look at similar questions and some documentation, but still can't figure it out.我看过类似的问题和一些文档,但仍然无法弄清楚。

if d is your dict then Try this:如果d是你的字典,那么试试这个:

d['response']['emails'] = [i['Address'] for i in d['response']['Email']]
del d['response']['Email']

Or you can do it using pop in just one line:或者你可以在一行中使用pop来完成:

d['response']['emails'] = [i['Address'] for i in d['response'].pop('Email')]

This will give you:这会给你:

 {'input_text': 'some text',
 'lang': 'eng',
 'response': {'Certification': [{'CertificationName': 'some_name'}],
  'ExecutiveSummary': ' text',
  'FamilyName': 'Smith',
  'FormattedName': 'John',
  'GivenName': 'John',
  'emails': ['someaddress@gmail.com']}}

I wouldn't rename a key, but you could just create 'emails' and then populate it:我不会重命名密钥,但您可以创建“电子邮件”然后填充它:

test_dict = {
    'input_text': 'some text',
    'lang': 'eng',
    'response': {
        'Certification': [{
            'CertificationName': 'some_name'
        }],
        'Email': [{
            'Address': 'someaddress@gmail.com',
            'Label': 'personal'
        }],
        'ExecutiveSummary': ' text',
        'FamilyName': 'Smith',
        'FormattedName': 'John',
        'GivenName': 'John'
    }
}
test_dict['response']['emails'] = [
    email['Address']
    for email in test_dict['response']['Email']
]

Edit: And as @h4z3 had mentioned, you can delete using del or if you need to get the value whilst deleting it pop编辑:正如@h4z3 所提到的,您可以使用del或者如果您需要在删除时获取该值pop

# Using del
del test_dict['response']['Email']

# Using pop
test_dict['response'].pop('Email')

If you know that there will be only 'Email' :如果您知道只有'Email'

from ast import literal_eval

od = {'input_text': 'some text',
 'lang': 'eng',
 'response': {'Certification': [{'CertificationName': 'some_name'}],
  'Email': [{'Address': 'someaddress@gmail.com', 'Label': 'personal'}],
  'ExecutiveSummary': ' text',
  'FamilyName': 'Smith',
  'FormattedName': 'John',
  'GivenName': 'John'}}

od['response']['Email'] = [od['response']['Email'][0]['Address']]
print(literal_eval(str(od).replace('Email:', 'email:')))

{'input_text': 'some text',
 'lang': 'eng',
 'response': {'Certification': [{'CertificationName': 'some_name'}],
  'Email': ['someaddress@gmail.com'],
  'ExecutiveSummary': ' text',
  'FamilyName': 'Smith',
  'FormattedName': 'John',
  'GivenName': 'John'}}

You can make new key emails and after that delete the old one:您可以制作新的关键emails ,然后删除旧的emails

>>> my_dict = {
...     'input_text': 'some text',
...     'lang': 'eng',
...     'response': {
...         'Certification': [
...             {
...                 'CertificationName': 'some_name'
...             }
...         ],
...         'Email': [
...             {
...                 'Address': 'someaddress@gmail.com', 
...                 'Label': 'personal'
...             }
...         ],
...         'ExecutiveSummary': ' text',
...         'FamilyName': 'Smith',
...         'FormattedName': 'John',
...         'GivenName': 'John'
...     }
... }
>>> my_dict['response']['emails'] = [d['Address'] for d in my_dict['response']['Email']]
>>> my_dict['response'].pop('Email', None)
>>> print(my_dict)
    {
...     'input_text': 'some text',
...     'lang': 'eng',
...     'response': {
...         'Certification': [
...             {
...                 'CertificationName': 'some_name'
...             }
...         ],
...         'ExecutiveSummary': ' text',
...         'FamilyName': 'Smith',
...         'FormattedName': 'John',
...         'GivenName': 'John',
...         'emails': ['someaddress@gmail.com']
...     }
... }

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

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