我有一个Python脚本,我正在深入研究与一个列表相关的几个JSON字典,我引用了这个线程来访问字典。
我的每个输出都来自列表的第0个字典; 这是一个结构的例子,说明了我为什么要索引这些值;所以在我的脚本中我想使用第0个索引来访问第1个字典的项目,第2个的第1个索引,依此类推。在这种情况下,容器编号,但这样做时我的输出垂直打印。
{
"ListOfLa311Containers": {
"La311Containers": [
{
"ActiveStatus": "Y",
"CollectionLocation": "Curb",
"ContactFirstName": "BOSSupervisor",
"ContactLastName": "CallCenter",
"ContainerNumber": "6356543654445",
"ContainerSize": "30Gallon(Small)Black",
"ContainerType": "BlackRefuse",
"DamageonAxle": "",
"DamageonBody": "",
"DamageonHandleEndcap": "",
"DamageonLid": "",
"DamageonWheels": "",
"DeliveryReason": "Exchange",
"DriverFirstName": "",
"DriverLastName": "",
"ExchangeDetails": "",
"GatedCommunityMultifamilyDwelling": "",
"LastUpdatedBy": "SANCID132",
"MobileHomeSpace": "",
"Name": "062420151714334791",
"PickupReason": "",
"PurposeofSR": "",
"RequestFor": "Delivery",
"ServiceDateRendered": "",
"TruckNo": "",
"Type": "Containers"
},
{
"ActiveStatus": "Y",
"CollectionLocation": "Curb",
"ContactFirstName": "BOSSupervisor",
"ContactLastName": "CallCenter",
"ContainerNumber": "3543545445234324653",
"ContainerSize": "60Gallon(Small)Black",
"ContainerType": "BlackRefuse",
"DamageonAxle": "",
"DamageonBody": "",
"DamageonHandleEndcap": "",
"DamageonLid": "",
"DamageonWheels": "",
"DeliveryReason": "Exchange",
"DriverFirstName": "",
"DriverLastName": "",
"ExchangeDetails": "",
"GatedCommunityMultifamilyDwelling": "",
"LastUpdatedBy": "SANCID132",
"MobileHomeSpace": "",
"Name": "062420151714334791",
"PickupReason": "",
"PurposeofSR": "",
"RequestFor": "Delivery",
"ServiceDateRendered": "",
"TruckNo": "",
"Type": "Containers"
}
]
}
}
How to access a dictionary key value present inside a list?
脚本:
try:
k_container = ' '
k_container_size = ' '
container_info = " "
for sr in Containers:
if("La311Containers" in Containers):
lcontainers311 = Containers ["La311Containers"]
for Containers in lcontainers311:
k_container_size_1= Containers['ContainerSize']
k_container_type_1 = Containers['ContainerType']
k_damage_lid_1 = Containers['DamageonLid']
k_damage_axle_1 = Containers['DamageonAxle']
k_damage_on_body_1 = Containers['DamageonBody']
k_damage_on_handle_1 = Containers['DamageonHandleEndcap']
k_damage_on_wheels_1 = Containers ['DamageonWheels']
k_container_number_1= Containers['ContainerNumber']
# print k_container_number_1
for container_number in k_container_number_1:
print container_number[0]
输出:
r
6
b
7
6
5
8
9
8
7
6
n
/
a
o
9
r
1
6
5
2
5
5
5
R
9
R
0
1
1
5
4
2
0
4
5
R
6
B
1
2
3
4
5
6
7
8
9
4
n
/
a
r
6
m
0
7
1
7
1
5
6
4
1
R
3
G
1
2
3
4
5
1
2
3
4
5
R
6
B
0
1
0
3
7
8
9
4
5
6
R
6
B
0
1
0
3
5
2
4
6
9
8
R
9
B
1
2
3
7
6
4
4
3
6
r
6
g
0
1
0
2
0
3
0
6
9
1
r
3
b
o
1
0
2
3
6
9
7
r
6
m
6
8
9
4
4
3
8
5
N
/
A
N
/
a
r
6
b
1
2
3
4
6
9
6
r
6
b
1
2
3
4
5
6
7
R
9
R
0
1
0
2
0
3
5
6
7
8
R
9
R
1
2
3
4
5
6
7
8
9
r
9
b
0
8
0
1
5
4
6
4
n
/
a
r
6
b
7
0
5
0
0
5
6
5
6
n
/
a
r
6
b
1
2
4
1
2
4
1
4
6
7
r
9
g
0
3
0
0
0
0
0
2
2
2
R
6
B
0
1
0
3
0
5
2
6
3
9
R
9
B
1
2
3
5
3
1
6
5
r
6
b
1
2
3
4
5
6
7
8
N
/
A
R
9
B
1
2
0
1
2
3
6
7
7
R
6
R
0
1
0
7
1
1
1
1
1
1
R
6
B
0
7
0
7
1
5
2
5
2
9
R
6
B
0
7
0
7
1
5
2
5
8
6
R
9
R
0
7
0
4
0
0
0
0
3
4
n
/
a
n
/
a
n
/
a
n
/
a
1
1
1
1
1
1
1
1
1
1
1
1
1
n
/
a
n
/
a
r
2
A
1
1
1
1
1
1
1
1
1
1
r
3
a
3
5
3
4
5
3
5
3
4
5
r
3
v
3
2
2
2
2
2
2
2
2
2
8
8
8
8
8
8
8
8
8
8
8
8
8
R
3
B
1
2
3
4
1
2
3
4
答案 0 :(得分:0)
这是因为print命令会自动添加换行符。 一个很好的黑客将是使用:
answer=""
for container_number in k_container_number_1:
answer+=str(container_number[0])
print answer
另一种方法是使用标准输出:
import sys
for container_number in k_container_number_1:
sys.stdout.write(container_number[0])
修改强> 如果我正确理解了问题,您需要检查容器是否是第一个。因此,我用“if”语句替换了你的“for”循环。这是代码:
try: k_container = ' ' k_container_size = ' ' container_info = " " for sr in Containers: if("La311Containers" in Containers): lcontainers311 = Containers ["La311Containers"] for Containers in lcontainers311: k_container_size_1= Containers['ContainerSize'] k_container_type_1 = Containers['ContainerType'] k_damage_lid_1 = Containers['DamageonLid'] k_damage_axle_1 = Containers['DamageonAxle'] k_damage_on_body_1 = Containers['DamageonBody'] k_damage_on_handle_1 = Containers['DamageonHandleEndcap'] k_damage_on_wheels_1 = Containers ['DamageonWheels'] k_container_number_1= Containers['ContainerNumber'] # print k_container_number_1 if Containers==lcontainers311[0]: print k_container_number_1
答案 1 :(得分:0)
以下是您的问题:
for container_number in k_container_number_1:
print container_number[0]
您正在迭代k_container_number_1
,这是一个包含容器编号的字符串。对字符串的迭代会产生字符串中的各个字符。因此container_number
是容器编号的每个字符。 Python正在自己的行上打印每个字符,就像你告诉它的那样。
解决方案:如果您不希望每个字符都打印在自己的行上,请不要在自己的行上打印每个字符。用以下代码替换不必要的循环:
print k_container_number_1
此外,这应该缩进一个额外的级别,以便它在for Containers in lcontainers311
循环内,从而打印每个容器的容器号。根据您的输出,它似乎已经打印了所有容器编号,这意味着您实际运行的代码不是您在此处发布的代码。