如何以Python方式将字典值映射到另一个字典?

时间:2020-10-09 21:40:51

标签: python dictionary

我有以下字典:

source_dictionary = {
   "id": "1",
   "duration": "3",
   "start_time": "2020-08-31T12:22:11.8000000-0400",
   "end_time": "2020-08-31T12:24:36.8000000-0400",
   "customer_id": "123",
   "customer_extension": "123455",
   "customer_fullname": "John Doe",
   "call_reason": "complaint",
   "region": "NY",
   "company_number": "456",
   "language": "English",
   "company_phone_number": "999999",
   "customer_phone_number": "11111",
   "call_number": "20",
   "organization_name": "AAA",
   "company_name": "BBB"
   } 

我想创建一个重命名几个键的新字典,将其中一个键的值转换为整数,然后将函数应用于start_timeend_time以及其余键/值对保持不变。

结果字典为:

   {
   "duration": 3,
   "start_datetime": "2020-08-31T16:22:11.800000+00:00",
   "end_datetime": "2020-08-31T16:24:36.800000+00:00",
   "customer_id": "123",
   "customer_extension": "123455",
   "customer_full_name": "John Doe",
   "call_reason": "complaint",
   "region": "NY",
   "company_number": "456",
   "language": "English",
   "company_phone_number": "999999",
   "customer_phone_number": "11111",
   "call_number": "20",
   "organization_name": "AAA",
   "company_name": "BBB"
   } 

我当前的代码是:

    def convert_iso_to_utc(date_string):
        yourdate = dateutil.parser.parse(date_string)
        return yourdate.astimezone(timezone.utc).isoformat()

    new_dict = {}

    new_dict["duration"] = int(source_dict["duration"])
    new_dict["start_datetime"] = convert_iso_to_utc(source_dict["start_time"])
    new_dict["end_datetime"] = convert_iso_to_utc(source_dict["end_time"])
    new_dict["customer_id"] = source_dict["customer_id"]
    new_dict["customer_extension"] = source_dict["customer_extension"]
    new_dict["customer_full_name"] = source_dict["customer_fullname"]
    new_dict["call_reason"] = source_dict["call_reason"]
    new_dict["region"] = source_dict["region"]
    new_dict["company_number"] = source_dict["company_number"]
    new_dict["distributor_number"] = source_dict["distributor_number"]
    new_dict["language"] = source_dict["language"]
    new_dict["company_phone_number"] = source_dict["company_phone_number"]
    new_dict["call_number"] = source_dict["call_number"]
    new_dict["organization_name"] = source_dict["organization_name"]
    new_dict["company_name"] = source_dict["company_name"]

是否有更Python化的方式做到这一点?

2 个答案:

答案 0 :(得分:0)

由于您经常复制source_dict中的项目,并且由于您对datetime条目执行了相同的操作,因此可以执行循环:

new_dict = {}

for key, value in source_dict.items():
    # "id" was left out in your example
    if key == "id":
        continue
    elif key == "duration":
        new_dict[key] = int(value)
    elif key in ("start_datetime", "end_datetime"):
        new_dict[key] = convert_iso_to_utc(value)
    else:
        new_dict[key] = value

答案 1 :(得分:0)

您可以copy原始词典并在复制的版本中更改所需的值。

new_dict = source_dict.copy()
new_dict["duration"] = int(new_dict["duration"])
new_dict["start_datetime"] = convert_iso_to_utc(new_dict["start_time"])
new_dict["end_datetime"] = convert_iso_to_utc(new_dict["end_time"])