Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/19.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/visual-studio-2008/2.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_Python 3.x_Dictionary_Pydash - Fatal编程技术网

在python字典中设置内部值,而不声明多个空字典

在python字典中设置内部值,而不声明多个空字典,python,python-3.x,dictionary,pydash,Python,Python 3.x,Dictionary,Pydash,假设我有一本像这样的字典 ship = {'AddNewShipmentV3': {'oShipData': {'ReadyDate': '2021-01-11T12:00:00', 'CloseTime': '2021-01-11T08:12:34', 'ServiceLevel': 'EC', 'ShipperName': 'Test 1/12/2021','SpecialInstructions': 'Invoice - RMA#S, 7800401086-GOOD EQUIP Picku

假设我有一本像这样的字典

ship = {'AddNewShipmentV3': {'oShipData': {'ReadyDate': '2021-01-11T12:00:00', 'CloseTime': '2021-01-11T08:12:34', 'ServiceLevel': 'EC', 'ShipperName': 'Test 1/12/2021','SpecialInstructions': 'Invoice - RMA#S, 7800401086-GOOD EQUIP Pickup - DRIVER WILL NEED SHRINK WRAP FOR 2 PALLETS PLEASE CALL', 'Station': 'SLC', 'CustomerNo': '9468', 'BillToAcct': '9468', 'DeclaredType': 'LL'}}}
我想通过提取ReadyDate值并将其存储为ReadyTime和CLoseTime值并将其存储在CloseDate中来创建一个新字典(b)。我想把这些新的键值对添加到字典的开头。我试过这个

def ready_date_time(a):
 b = {}
 b["AddNewShipmentV3"] = {}
 b["AddNewShipmentV3"]["oShipData"] = {}
 b["AddNewShipmentV3"]["oShipData"]["ReadyTime"] = a["AddNewShipmentV3"]["oShipData"]["ReadyDate"]
    
 if "CloseTime" in a["AddNewShipmentV3"]["oShipData"]:
    b["AddNewShipmentV3"]["oShipData"]["CloseDate"] = a["AddNewShipmentV3"]["oShipData"]["CloseTime"]    
 else:
    pass           
 # this will create a new dictionary (b) with new values "CloseDate" and "ReadyTime" in the starting 
 of the dictionary
 b["AddNewShipmentV3"]["oShipData"].update(a["AddNewShipmentV3"]["oShipData"])    
 return b
 
read_date_time(ship)
我的输出:

{'AddNewShipmentV3': {'oShipData': {'ReadyTime': '2021-01-11T12:00:00', 'CloseDate': '2021-01-11T08:12:34', 'ReadyDate': '2021-01-11T12:00:00', 'CloseTime': '2021-01-11T08:12:34', 'ServiceLevel': 'EC', 'ShipperName': 'Test 1/12/2021', 'SpecialInstructions': 'Invoice - RMA#S, 7800401086-GOOD EQUIP Pickup - DRIVER WILL NEED SHRINK WRAP FOR 2 PALLETS PLEASE CALL', 'Station': 'SLC', 'CustomerNo': '9468', 'BillToAcct': '9468', 'DeclaredType': 'LL'}}}
我希望在不声明多个空dict来设置内部值的情况下完成此任务。
我在某个地方读到过它,我们可以使用pydash来完成它,但不熟悉如何使用它

可以使用python集合中的defaultdict类来完成

from collections import defaultdict

d = defaultdict(lambda: defaultdict(dict))
d['a']['b']['c'] = 'd'

你期望的结果是什么