我有一个json,它可以在列表中包含多达10个嵌套字典。这10个嵌套字典都与一个服务请求号相关联,但每个字典的请求不同。例如,地址一的服务请求1可能包含[容器]的列表,并且在此容器列表中{key1:value1,key2,value2,key3:value3}等等,最多10次。
如何根据字典中出现的数字成功将这些值附加到一个字段,即value1 = containercolor1,value2 = containercolor2,value3 = containercolor3? 请参阅下面的示例,这个相同的示例适用于我正在使用的下面json中的多个字段。
我基本上想要访问每个列表中的每个键/值对,例如
for k_container in k_container_number_1:
print k_container[0]
打印:
的r 9 G 0 7 8 7 7 8 6 8 7 五 [R 9 乙 0 7 8 7 7 8 6 7 五 五 [R 6 乙 1 2 3 4 8 7 8 7 6 7
ř 9 b 0 五 3 0 2 2`
如何解决这个问题,以便我的角色不会循环或水平?
JSON示例:
{
"Response": {
"LastPage": "false",
"NumOutputObjects": "1",
"ListOfServiceRequest": {
"ServiceRequest": [
{
"AddressVerified": "Y",
"SRNumber": "1-20229961",
"ListOfLa311Containers": {
"La311Containers": [
{
"ContainerNumber": "R9G0665765765",
"ContainerType": "Green Yard Trimmings",
"DamageonLid": "N",
"DamageonAxle": "N",
"DamageonBody": "N",
"DamageonWheels": "Y",
"DeliveryReason": "",
"DriverFirstName": "",
"DriverLastName": "",
"ExchangeDetails": "",
"GatedCommunityMultifamilyDwelling": "",
"MobileHomeSpace": "",
"PickupReason": "",
"RequestFor": "Damage",
"ServiceDateRendered": "",
"TruckNo": "",
"Type": "Containers",
"CollectionLocation": "Curb",
"ContainerSize": "90 Gallon (Large) Green",
"LastUpdatedBy": "370758",
"ActiveStatus": "Y",
"PurposeofSR": "",
"DamageonHandleEndcap": "N",
"ContactFirstName": "Shawnae",
"ContactLastName": "Muck",
"Name": "062520151201141851"
},
{
"ContainerNumber": "R9R0877765456",
"ContainerType": "Blue Recycling",
"DamageonLid": "Y",
"DamageonAxle": "N",
"DamageonBody": "Y",
"DamageonWheels": "N",
"DeliveryReason": "",
"DriverFirstName": "",
"DriverLastName": "",
"ExchangeDetails": "",
"GatedCommunityMultifamilyDwelling": "",
"MobileHomeSpace": "",
"PickupReason": "",
"RequestFor": "Damage",
"ServiceDateRendered": "",
"TruckNo": "",
"Type": "Containers",
"CollectionLocation": "Curb",
"ContainerSize": "90 Gallon (Large) Blue",
"LastUpdatedBy": "370758",
"ActiveStatus": "Y",
"PurposeofSR": "",
"DamageonHandleEndcap": "N",
"ContactFirstName": "Shawnae",
"ContactLastName": "Muck",
"Name": "062520151201141852"
},
{
"ContainerNumber": "R9B0746754645",
"ContainerType": "Black Refuse",
"DamageonLid": "N",
"DamageonAxle": "N",
"DamageonBody": "N",
"DamageonWheels": "N",
"DeliveryReason": "",
"DriverFirstName": "",
"DriverLastName": "",
"ExchangeDetails": "",
"GatedCommunityMultifamilyDwelling": "",
"MobileHomeSpace": "",
"PickupReason": "",
"RequestFor": "Damage",
"ServiceDateRendered": "",
"TruckNo": "",
"Type": "Containers",
"CollectionLocation": "Curb",
"ContainerSize": "90 Gallon (Large) Black",
"LastUpdatedBy": "370758",
"ActiveStatus": "Y",
"PurposeofSR": "",
"DamageonHandleEndcap": "Y",
"ContactFirstName": "Shawnae",
"ContactLastName": "Muck",
"Name": "062520151201141853"
}
]
}
}
]
}
}
}
要附加到NumPy数组的当前代码:
Containerdt = np.dtype([('Address', 'U128'),
('Y_CoordShape', '<f8'),
('X_CoordShape', '<f8'),
('Y_COR', '<f8'),
('X_COR', '<f8'),
('ReasonCode','U128'),
('NUMBERCYLA', 'U128'),
('SRNumber', 'U128'),
('Name', 'U128'),
('RESOLUTION_CODE','U128'),
('HOME_PHONE', 'U40'),
('CreatedDate', 'U128'),
('UpdatedDate', 'U128'),
# ('ItemDesc', 'U128'),
('SCHED_DATE', 'U128'),
(ContainerNumber01, 'U128'),
(ContainerNumber02, 'U128'),
(ContainerNumber03, 'U128'),
])
containerItems.append((ContainerAddress,
container_x,
container_y,
container_x,
container_y,
Container_ReasonCode,
Container_SRNumber,
Container_SRNumber,
Container_FullName,
Container_ResolutionCode,
Container_HomePhone,
Container_CreatedDate,
Container_UpdatedDate,
# "ItemDesc",
date_object,
"R9G0665765765", #FROM 1st Dictionary under ['ListofLa311Containers']['La311Containers']
"R9R0877765456", #FROM 2nd Dictionary under ['ListofLa311Containers']['La311Containers']
"R9B0746754645", #FROM 3RD Dictionary under ['ListofLa311Containers']['La311Containers']
))
答案 0 :(得分:0)
containerItems
是列表还是数组(具有这种复杂的dtype)?
如果是清单,可以。如果是阵列,那你就是在滥用它。语法错误,使用上下文错误。
我没有试图弄清楚你想要做什么,我建议你写一个函数(或两个)从列表中取出一个字典,并挑选出欲望的部分,将它们返回到一个合适的元组中。您可以根据需要在该函数中打包尽可能多的逻辑。
在这个阶段,您应该考虑处理字典,并生成列表和/或元组。不要将自己与结构化阵列业务混淆。构造结构化数组的最佳方法是在最后,给np.array
一个元组列表和一个dtype。不要通过附加元组来逐步构造它。