Python 使用if语句循环嵌套字典

Python 使用if语句循环嵌套字典,python,dictionary,Python,Dictionary,我有很多内部字典,我的逻辑有点不正确,因为我的输出接近我想要完成的,但还没有达到。我想连接来自内部字典的两个变量的值 虽然在循环中,我的变量打印正确,但当我使用另一个变量连接这两个变量时,我只收到几个值。此数据来自JSON结构 代码: BulkyItemInfo的输出: 1, Carpet 1, Carpet 1, Chair 1, Carpet 1, Chair 1, Desk 1, Carpet 1, Chair 1, Desk 1, Wood Bundles 1, Car

我有很多内部字典,我的逻辑有点不正确,因为我的输出接近我想要完成的,但还没有达到。我想连接来自内部字典的两个变量的值

虽然在循环中,我的变量打印正确,但当我使用另一个变量连接这两个变量时,我只收到几个值。此数据来自JSON结构

代码:

BulkyItemInfo的输出:

 1, Carpet 
 1, Carpet 1, Chair 
 1, Carpet 1, Chair 1, Desk 
 1, Carpet 1, Chair 1, Desk 1, Wood Bundles 
 1, Carpet 1, Chair 1, Desk 1, Wood Bundles 1, Other 
 1, Bookcase 
 1, Bicycle 
 2, Carpet 
 2, Carpet 2, Chair 
 2, Carpet 2, Chair 2, Headboard 
 1, Bicycle 
 1, Bicycle 1, Bird Cage (Plastic) 
 1, Bicycle 1, Bird Cage (Plastic) 1, Blinds 
 1, Bicycle 1, Bird Cage (Plastic) 1, Blinds 1, Bookcase 
 1, Bicycle 1, Bird Cage (Plastic) 1, Blinds 1, Bookcase 1, Dresser 
 1, Bicycle 1, Bird Cage (Plastic) 1, Blinds 1, Bookcase 1, Dresser 1, Entertainment Center 
 1, Bookcase 
 1, Bookcase 1, Desk 
 1, Bookcase 1, Desk 1, Shelf
打印在各自回路中时的数量和类型输出

1
File Cabinet (Wood)
1
Carpet
Chair
Desk
Wood Bundles
Other
1
Bookcase
1
Bicycle
5
6
2
Carpet
Chair
Headboard
Json示例:

{
    "ListOfLa311BulkyItem": {
        "BulkyItem": [
            {
                "BulkyItemCount": "5",
                "BulkyItemType": "Carpet",
                "CollectionLocation": "Curb",
                "IllegallyDumped": "N",
                "OtherBulkyItem": "",
                "Type": "Bulky Items",
                "SpecialRule": "Y",
                "GatedCommunityMultifamilyDwelling": "",
                "MobileHomeSpace": "",
                "IllegalDumpCollectionLoc": "",
                "LastUpdatedBy": "52299",
                "DriverFirstName": "",
                "DriverLastName": "",
                "ServiceDateRendered": "",
                "TruckNo": "",
                "ActiveStatus": "Y",
                "IllegalBulkyItemType": "",
                "PurposeofSR": "",
                "Name": "061720151151242471"
            },
            {
                "BulkyItemCount": "6",
                "BulkyItemType": "Chair",
                "CollectionLocation": "Curb",
                "IllegallyDumped": "N",
                "OtherBulkyItem": "",
                "Type": "Bulky Items",
                "SpecialRule": "",
                "GatedCommunityMultifamilyDwelling": "",
                "MobileHomeSpace": "",
                "IllegalDumpCollectionLoc": "",
                "LastUpdatedBy": "52299",
                "DriverFirstName": "",
                "DriverLastName": "",
                "ServiceDateRendered": "",
                "TruckNo": "",
                "ActiveStatus": "Y",
                "IllegalBulkyItemType": "",
                "PurposeofSR": "",
                "Name": "061720151151242472"
            },
            {
                "BulkyItemCount": "2",
                "BulkyItemType": "Headboard",
                "CollectionLocation": "Curb",
                "IllegallyDumped": "N",
                "OtherBulkyItem": "",
                "Type": "Bulky Items",
                "SpecialRule": "Y",
                "GatedCommunityMultifamilyDwelling": "",
                "MobileHomeSpace": "",
                "IllegalDumpCollectionLoc": "",
                "LastUpdatedBy": "52299",
                "DriverFirstName": "",
                "DriverLastName": "",
                "ServiceDateRendered": "",
                "TruckNo": "",
                "ActiveStatus": "Y",
                "IllegalBulkyItemType": "",
                "PurposeofSR": "",
                "Name": "061720151151242483"
            }
        ]
    }
}
上述json的所需输出

5, Carpet, 6, Chair, 2 Headboard

我不确定您到底需要什么,但这段代码从给定的json字符串中提供了所需的输出:

import json

jsonstr = '''
{
    "ListOfLa311BulkyItem": {
        "BulkyItem": [
            {
                "BulkyItemCount": "5",
                "BulkyItemType": "Carpet",
                "CollectionLocation": "Curb",
                "IllegallyDumped": "N",
                "OtherBulkyItem": "",
                "Type": "Bulky Items",
                "SpecialRule": "Y",
                "GatedCommunityMultifamilyDwelling": "",
                "MobileHomeSpace": "",
                "IllegalDumpCollectionLoc": "",
                "LastUpdatedBy": "52299",
                "DriverFirstName": "",
                "DriverLastName": "",
                "ServiceDateRendered": "",
                "TruckNo": "",
                "ActiveStatus": "Y",
                "IllegalBulkyItemType": "",
                "PurposeofSR": "",
                "Name": "061720151151242471"
            },
            {
                "BulkyItemCount": "6",
                "BulkyItemType": "Chair",
                "CollectionLocation": "Curb",
                "IllegallyDumped": "N",
                "OtherBulkyItem": "",
                "Type": "Bulky Items",
                "SpecialRule": "",
                "GatedCommunityMultifamilyDwelling": "",
                "MobileHomeSpace": "",
                "IllegalDumpCollectionLoc": "",
                "LastUpdatedBy": "52299",
                "DriverFirstName": "",
                "DriverLastName": "",
                "ServiceDateRendered": "",
                "TruckNo": "",
                "ActiveStatus": "Y",
                "IllegalBulkyItemType": "",
                "PurposeofSR": "",
                "Name": "061720151151242472"
            },
            {
                "BulkyItemCount": "2",
                "BulkyItemType": "Headboard",
                "CollectionLocation": "Curb",
                "IllegallyDumped": "N",
                "OtherBulkyItem": "",
                "Type": "Bulky Items",
                "SpecialRule": "Y",
                "GatedCommunityMultifamilyDwelling": "",
                "MobileHomeSpace": "",
                "IllegalDumpCollectionLoc": "",
                "LastUpdatedBy": "52299",
                "DriverFirstName": "",
                "DriverLastName": "",
                "ServiceDateRendered": "",
                "TruckNo": "",
                "ActiveStatus": "Y",
                "IllegalBulkyItemType": "",
                "PurposeofSR": "",
                "Name": "061720151151242483"
            }
        ]
    }
}
'''

dic = json.loads(jsonstr)

astr = ""
for listofbulkyitems in dic.itervalues():
    for bulkyitems in listofbulkyitems.itervalues():
        for bulkyitem in bulkyitems:
            if astr:
                astr += ", "     ## The string should not start by a comma but all the following items should be preceded by a comma
            astr += bulkyitem['BulkyItemCount']+", "+bulkyitem['BulkyItemType']
print astr
## prints '5, Carpet, 6, Chair, 2, Headboard'

请注意,我选择在打印之前将所有内容存储在字符串中。这不是必须的,但它是获得所需输出的一种简单方法。

为什么不使用标准库的模块?我认为您完全误解了这一点。原因可能是您没有明确说明您想要实现什么或您的问题是什么。那么,请告诉我,问题中显示了所需的输出,从上面的JSON中,我想循环并获得一个格式化的字符串。我能够迭代json,但我的逻辑在循环中不正确。我从一个web服务中使用这个json,并且已经在使用json模块了,所以这些信息是非常无用的。你的问题仍然不清楚你想要实现什么。只要记住你深陷其中,我们不是。请提供一份报告。
import json

jsonstr = '''
{
    "ListOfLa311BulkyItem": {
        "BulkyItem": [
            {
                "BulkyItemCount": "5",
                "BulkyItemType": "Carpet",
                "CollectionLocation": "Curb",
                "IllegallyDumped": "N",
                "OtherBulkyItem": "",
                "Type": "Bulky Items",
                "SpecialRule": "Y",
                "GatedCommunityMultifamilyDwelling": "",
                "MobileHomeSpace": "",
                "IllegalDumpCollectionLoc": "",
                "LastUpdatedBy": "52299",
                "DriverFirstName": "",
                "DriverLastName": "",
                "ServiceDateRendered": "",
                "TruckNo": "",
                "ActiveStatus": "Y",
                "IllegalBulkyItemType": "",
                "PurposeofSR": "",
                "Name": "061720151151242471"
            },
            {
                "BulkyItemCount": "6",
                "BulkyItemType": "Chair",
                "CollectionLocation": "Curb",
                "IllegallyDumped": "N",
                "OtherBulkyItem": "",
                "Type": "Bulky Items",
                "SpecialRule": "",
                "GatedCommunityMultifamilyDwelling": "",
                "MobileHomeSpace": "",
                "IllegalDumpCollectionLoc": "",
                "LastUpdatedBy": "52299",
                "DriverFirstName": "",
                "DriverLastName": "",
                "ServiceDateRendered": "",
                "TruckNo": "",
                "ActiveStatus": "Y",
                "IllegalBulkyItemType": "",
                "PurposeofSR": "",
                "Name": "061720151151242472"
            },
            {
                "BulkyItemCount": "2",
                "BulkyItemType": "Headboard",
                "CollectionLocation": "Curb",
                "IllegallyDumped": "N",
                "OtherBulkyItem": "",
                "Type": "Bulky Items",
                "SpecialRule": "Y",
                "GatedCommunityMultifamilyDwelling": "",
                "MobileHomeSpace": "",
                "IllegalDumpCollectionLoc": "",
                "LastUpdatedBy": "52299",
                "DriverFirstName": "",
                "DriverLastName": "",
                "ServiceDateRendered": "",
                "TruckNo": "",
                "ActiveStatus": "Y",
                "IllegalBulkyItemType": "",
                "PurposeofSR": "",
                "Name": "061720151151242483"
            }
        ]
    }
}
'''

dic = json.loads(jsonstr)

astr = ""
for listofbulkyitems in dic.itervalues():
    for bulkyitems in listofbulkyitems.itervalues():
        for bulkyitem in bulkyitems:
            if astr:
                astr += ", "     ## The string should not start by a comma but all the following items should be preceded by a comma
            astr += bulkyitem['BulkyItemCount']+", "+bulkyitem['BulkyItemType']
print astr
## prints '5, Carpet, 6, Chair, 2, Headboard'