Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/292.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 将字典作为具有不同键名的参数传递_Python_Dictionary - Fatal编程技术网

Python 将字典作为具有不同键名的参数传递

Python 将字典作为具有不同键名的参数传递,python,dictionary,Python,Dictionary,我对python还比较陌生,而且我在思考python的方式时仍然有问题。 我有一个函数,它具有CustomerAdd(id\u类型、id、姓名、电子邮件、地址、电话号码),并希望将客户作为参数传递:CustomerAdd(**customer) 我遇到的问题是,预期的客户格式是: { "name": [ "CustName" ], "address": [ "CustAddress" ], "id_type": "passport", "id": "123

我对python还比较陌生,而且我在思考python的方式时仍然有问题。 我有一个函数,它具有
CustomerAdd(id\u类型、id、姓名、电子邮件、地址、电话号码)
,并希望将客户作为参数传递:
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\u name和customer\u address的键名,并将值的类型更改为列表

我可以通过以下方式更改密钥名称:

customer = old_customer.copy()
customer['name'] = customer.pop('customer_name')
customer['address'] = customer.pop('customer_address')
但仍然无法更改值类型。有什么想法吗?或者用其他类似蟒蛇的方法来解决这个问题


谢谢

如果需要列表,只需将它们列为列表:

customer['name'] = [customer.pop('customer_name')]
customer['address'] = [customer.pop('customer_address')]

如果需要列表,只需将它们列为列表:

customer['name'] = [customer.pop('customer_name')]
customer['address'] = [customer.pop('customer_address')]