归一化Python中的字典/列表的混合列表

时间:2018-11-08 16:25:30

标签: python algorithm list dictionary

我有以下几种混合格式的大地址集:

1)房屋和公寓的简单直形变体:

“大区大街大街1号楼1号楼”,

“大区大街大街1号楼2室”

district
    street 
        house
            flat
            flat

2)房屋,公寓和建筑物的复杂变体:

“大区大街大街1号楼1号楼”

“大区大街大街1号楼2室”

“大区大街大街1号楼1号楼,公寓1”

“大区大街大街1号楼1号楼,公寓2”

(因此,有带有房屋的1号房和带有房屋的1号房1号楼)

district
    street
        house
            flat
            flat
            building
                flat
                flat

3)只有建筑物的房屋的变种

“大区大街大街1号楼1号楼,公寓1”

“大区大街大街1号楼1号楼,公寓2”

“大区大街大街1号楼2号楼,公寓1”

“大区大街大街1号楼2号楼,公寓2”

(在这种情况下,没有房子1的建筑物,只有房子1的建筑物1和房子1的建筑物2)

district
    street
        house
            building
                flat
                flat
            building
                flat
                flat

数据的结构如下:

    [
        {"text": "street 1",
         "level": 7,
        "children":[
            {"text": "house 1",
             "level":8,
             "children":[
                {"text": "flat 1", "level": 11},
                {"text": "flat 2", "level": 11}
             ]
            },
            {"text": "house 2",
             "level": 8,
             "children":[
                {"text": "building 1",
                 "level": 9,
                 "children":[
                    {"text": "flat 1", "level": 11}
                 ]
                },
                {"text": "flat 1", "level": 11}
             ]
            },
            {"text": "house 3",
             "children": []
            }
        ]
        }
    ]

我需要的是字典列表:

    [
    {"level 7": "Street 1", "level 8": "house 1", "level 9": NaN, "level 11":"flat 1"},
    {"level 7": "Street 1", "level 8": "house 1", "level 9": NaN, "level 11":"flat 2"},
    {"level 7": "Street 1", "level 8": "house 2", "level 9": "building 1", "level 11":"flat 1"},
    {"level 7": "Street 1", "level 8": "house 2", "level 9": NaN, "level 11":"flat 1"},
    {"level 7": "Street 1", "level 8": "house 3", "level 9": NaN, "level 11":NaN}
    ]

我真的很卡住如何制作这个算法。

0 个答案:

没有答案