根据另一个字典创建一个python字典

时间:2013-05-02 12:36:07

标签: python list dictionary

我有以下dict包含以下数据:

response = {"status":"ERROR","email":"EMAIL_INVALID","name":"NAME_INVALID"}

我正在尝试根据'响应'创建一个新的dict,看起来如下所示:

{'api_error': {'list': [{'converted_value': 'No special characters allowed.',
                         'field': 'name',
                         'value': 'NAME_INVALID'},
                        {'converted_value': 'invalid email',
                         'field': 'email',
                         'value': 'EMAIL_INVALID'}],
               'status': 'ERROR'},
 'email': 'EMAIL_INVALID',
 'email_label': 'invalid email',
 'name': 'NAME_INVALID',
 'name_label': 'No special characters allowed.',
 'status': 'ERROR'}

到目前为止,我已经能够做到以下几点:

ret = {}
for k in response:
        if k != 'status':
                ret[k+"_label"] = convert(response[k])
                ret[k] = response[k]
        else:
            ret[k] = convert(response[k])

其中'convert'函数转换每个响应值。例如,NAME_INVALID将转换为“不允许特殊字符”。等等。以下是上述代码的输出:

{"status":"ERROR","name_label":"No special characters allowed.",
"email_label":"invalid email","name":"NAME_INVALID","email":"EMAIL_INVALID"}

我在创建字典的其余部分时遇到问题。密钥是'api_error'的那个。最有效的方法是什么?

3 个答案:

答案 0 :(得分:1)

import pprint

response = {"status": "ERROR", "email": "EMAIL_INVALID", "name":
            "NAME_INVALID"}


def convert(label):
    return {'NAME_INVALID': 'No special characters allowed',
            'EMAIL_INVALID': 'invalid email',
            'ERROR': 'ERROR'}[label]

ret = {}
for k in response:
    if k != 'status':
        ret[k + "_label"] = convert(response[k])
        ret[k] = response[k]
        info = {'converted_value': ret[k + "_label"],
                'field': k,
                'value': response[k]}
        (ret.setdefault('api_error', {})
            .setdefault('list', [])
            .append(info))
    else:
        ret[k] = convert(response[k])
        ret.setdefault('api_error', {})['status'] = ret[k]
pprint.pprint(ret)

产量

{'api_error': {'list': [{'converted_value': 'invalid email',
                         'field': 'email',
                         'value': 'EMAIL_INVALID'},
                        {'converted_value': 'No special characters allowed',
                         'field': 'name',
                         'value': 'NAME_INVALID'}],
               'status': 'ERROR'},
 'email': 'EMAIL_INVALID',
 'email_label': 'invalid email',
 'name': 'NAME_INVALID',
 'name_label': 'No special characters allowed',
 'status': 'ERROR'}

答案 1 :(得分:0)

再做一个类似

的功能
def make2nddict(response):
for k in response:
    if k != 'status':
        d = {}
        d['converted_value'] = convert(k)
        d['field'] = k
        d['value'] = response[k]
        arr.append(d)
    else:
        final[k] = response[k]
final['list'] = arr

arr= []
final = {}

def convert(error):
    if error == 'NAME_INVALID':
        return 'No special characters allowed'
    elif error == 'EMAIL_INVALID':
        return 'EMAIL_INVALID'
    else:
        return error

ret = {}
for k in response:
    if k != 'status':
            ret[k+"_label"] = convert(response[k])
            ret[k] = response[k]
    else:
        ret[k] = convert(response[k])

在你的api_error字典中输入noth函数 祝你好运

答案 2 :(得分:0)

这里有完整的代码:---

>>>response = {"status":"ERROR","email":"EMAIL_INVALID","name":"NAME_INVALID"}
>>>def convert(parameter):
       if parameter == "NAME_INVALID":
           return "No special characters allowed."
       if parameter =="EMAIL_INVALID":
           return "invalid email"

>>>def newdic(response):
       ret={}
       response_keys = response.keys()
       response_keys.remove("status")
       if response['status']=="ERROR":
            ret = response
            ret['api_error'] ={'list':[],'status':"ERROR"}
            for key in response_keys:
                 ret[key+"_label"] = convert(response[key])
                 dic={}
                 dic['converted_value'] = convert(response[key])
                 dic['field'] = key
                 dic['value'] = response[key]
                 ret['api_error']['list'].append(dic)

       return ret

答案是: -

>>>newdic(response)

{'status': 'ERROR',
 'email_label': 'invalid email',
 'name': 'NAME_INVALID',
 'name_label': 'No special characters allowed.', 
 'api_error': {
 'email': 'EMAIL_INVALID',
 'list': [{'field': 'email', 'value': 'EMAIL_INVALID',
             'converted_value': 'invalid email'},
             {'field': 'name', 'value': 'NAME_INVALID',
             'converted_value': 'No special characters allowed.'}
            ],
 'status': 'ERROR'}
}