格式化字典以获取价值

时间:2018-12-16 16:21:02

标签: python

我有一个成员变量;

onload

其中客户端是另一个对象的ID。理想情况下,我想做类似的事情,但是由于变量是字典,所以不可能。

valid_add_customer_request = {
    "client": {0},
    "name": "CustomerName",
    "account_number": "W/L141123512",
    "mobile_number": "1232 414122",
    "landline_number": "1234515123",
    "email": "CustomerName@email.com"
}

有没有达到上述目标的干净方法。

3 个答案:

答案 0 :(得分:2)

valid_add_customer_request = lambda x: {
    "client": x,
    "name": "CustomerName",
    "account_number": "W/L141123512",
    "mobile_number": "1232 414122",
    "landline_number": "1234515123",
    "email": "CustomerName@email.com"
}

valid_add_customer_request(1)

答案 1 :(得分:1)

这可能会帮助

https://www.programiz.com/python-programming/methods/dictionary/update

valid_add_customer_request.update(dict(client=1))

答案 2 :(得分:1)

您可以执行以下操作之一,具体取决于所需的结果:

valid_add_customer_request['client'] = 1 # client = 1 i.e. int
valid_add_customer_request['client'] = {1} # client = {1} i.e. set with one element 1
valid_add_customer_request.setdefault('client', set()).add(1) # client = {0,1}, i.e. add element to the existing set