簡體   English   中英

在嵌套結構中編輯 python 字典鍵

[英]Editing python dictionary keys in nested structures

我有以下結構要編輯,但我不知道該怎么做。 例如,我需要將“電子郵件”重命名為“電子郵件”,並且值中只有電子郵件地址。

{'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'}
}

這是我想要的

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

我看過類似的問題和一些文檔,但仍然無法弄清楚。

如果d是你的字典,那么試試這個:

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

或者你可以在一行中使用pop來完成:

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

這會給你:

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

我不會重命名密鑰,但您可以創建“電子郵件”然后填充它:

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']
]

編輯:正如@h4z3 所提到的,您可以使用del或者如果您需要在刪除時獲取該值pop

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

# Using pop
test_dict['response'].pop('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'}}

您可以制作新的關鍵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