我对python相对较新,我仍然在思考pythonic方式时遇到问题。
我有一个具有CustomerAdd(id_type, id, name, email, adress, phone_number)
的函数,并且希望将客户作为参数传递给CustomerAdd(**customer)
。
我的问题是预期的客户格式为:
{
"name": [
"CustName"
],
"address": [
"CustAddress"
],
"id_type": "passport",
"id": "123123123",
"email": "test@email.com",
"phone_number": "123123"
}
我的客户输入如下:
{
"id_type": "passport",
"id": "123123123",
"customer_name": "Gordon Gekko",
"customer_address": "Fake Street 123",
"phone_number": "123456789",
"email": "customer@aol.com"
}
因此,如您所见,我需要更改customer_name和customer_address的键名,并将值的类型也更改为列表。
我可以使用以下方式更改键名:
customer = old_customer.copy()
customer['name'] = customer.pop('customer_name')
customer['address'] = customer.pop('customer_address')
但是仍然无法更改值类型。有任何想法吗?或任何其他pythonic方式来解决这个问题?
谢谢
答案 0 :(得分:3)
如果它们需要成为列表,只需将它们列出即可:
customer['name'] = [customer.pop('customer_name')]
customer['address'] = [customer.pop('customer_address')]