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

时间:2020-02-25 11:33:39

标签: python dictionary

我具有以下结构要编辑,但不确定如何执行。例如。我需要将“电子邮件”重命名为“电子邮件”,并且值中只有电子邮件地址。

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

我看过类似的问题和一些文档,但仍然无法弄清。

4 个答案:

答案 0 :(得分:3)

如果const movieTitleFormatter = title => { if(!title) { return ''; } let arr = []; let exludeWords = ['of', 'the'] arr = title.split(' '); return arr.map((word, i) => { return exludeWords.includes(word) && i!=0 ? [word] : word.charAt(0).toUpperCase() + word.slice(1); }).join(' '); } console.log(movieTitleFormatter('psycho')); //'Psycho'; console.log(movieTitleFormatter('silence of the lambs')); //'Silence of the Lambs'; console.log(movieTitleFormatter('the last emperor')); //'The Last Emperor'; //Here is shows wrong. console.log(movieTitleFormatter()); //'';是您的字典,请尝试以下操作:

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

答案 1 :(得分:1)

我不会重命名密钥,但是您可以创建“电子邮件”然后填充它:

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

答案 2 :(得分:0)

如果您知道只有'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'}}

答案 3 :(得分:0)

您可以创建新密钥5,然后删除旧密钥:

Feature: CSV Filter

    Background:
        * def data = read('testdata.csv')
        * def data = get data[?(@.testcase=='tc02')]

    Scenario Outline: <testcase>,<desc>
        * def look = "<testcase>,<desc>"
        * print look

        Examples:
            | data |

# testdata.csv
# testcase,desc
# tc01,desc01
# tc02,desc02
# tc03,desc03