递归地写字典

时间:2018-12-15 17:21:41

标签: python dictionary

我有一个包含位置数据的JSON文件。以下是该文件的示例。

[
{
    "id": 1,
    "name": "Western Cape",
    "filename": "1",
    "type": "Province",
    "typeCode": 1
},
{
    "id": 2,
    "name": "Eastern Cape",
    "filename": "2",
    "type": "Province",
    "typeCode": 1
},
{
    "id": 3,
    "name": "Northern Cape",
    "filename": "3",
    "type": "Province",
    "typeCode": 1
},
{
    "id": 4,
    "name": "Free State",
    "filename": "4",
    "type": "Province",
    "typeCode": 1
},
{
    "id": 5,
    "name": "KwaZulu-Natal",
    "filename": "5",
    "type": "Province",
    "typeCode": 1
},
{
    "id": 6,
    "name": "North West",
    "filename": "6",
    "type": "Province",
    "typeCode": 1
},
{
    "id": 7,
    "name": "Gauteng",
    "filename": "7",
    "type": "Province",
    "typeCode": 1
},
{
    "id": 8,
    "name": "Mpumalanga",
    "filename": "8",
    "type": "Province",
    "typeCode": 1
},
{
    "id": 9,
    "name": "Limpopo",
    "filename": "9",
    "type": "Province",
    "typeCode": 1
},
{
    "id": 199,
    "name": "City of Cape Town",
    "filename": "1.199",
    "type": "Metropolitan Municipality",
    "typeCode": 2,
    "parent": 1
},
{
    "id": 260,
    "name": "Buffalo City",
    "filename": "2.260",
    "type": "Metropolitan Municipality",
    "typeCode": 2,
    "parent": 2
},
{
    "id": 299,
    "name": "Nelson Mandela Bay",
    "filename": "2.299",
    "type": "Metropolitan Municipality",
    "typeCode": 2,
    "parent": 2
},
{
    "id": 499,
    "name": "Mangaung",
    "filename": "4.499",
    "type": "Metropolitan Municipality",
    "typeCode": 2,
    "parent": 4
},
{
    "id": 599,
    "name": "eThekwini",
    "filename": "5.599",
    "type": "Metropolitan Municipality",
    "typeCode": 2,
    "parent": 5
},
{
    "id": 797,
    "name": "Ekurhuleni",
    "filename": "7.797",
    "type": "Metropolitan Municipality",
    "typeCode": 2,
    "parent": 7
},
{
    "id": 798,
    "name": "City of Johannesburg",
    "filename": "7.798",
    "type": "Metropolitan Municipality",
    "typeCode": 2,
    "parent": 7
},
{
    "id": 799,
    "name": "City of Tshwane",
    "filename": "7.799",
    "type": "Metropolitan Municipality",
    "typeCode": 2,
    "parent": 7
}]

我希望实现以下输出:

{'Eastern Cape': {u'Buffalo City': {}, u'Nelson Mandela Bay': {}}, 'Gauteng': {u'Ekurhuleni': {}, u'City of Johannesburg': {}, u'City of Tshwane': {}}, 'North West': {}, 'Mpumalanga': {}, 'Limpopo': {}, 'Western Cape': {u'City of Cape Town': {}}, 'KwaZulu-Natal': {u'eThekwini': {}}, 'Northern Cape': {}, 'Free State': {u'Mangaung': {}}}

我编写了以下代码块来实现它:

province_dict = {}
final_dict = {
    'Western Cape': {},
    'Eastern Cape': {},
    'Northern Cape': {},
    'Free State': {},
    'KwaZulu-Natal': {},
    'North West': {},
    'Gauteng': {},
    'Mpumalanga': {},
    'Limpopo': {},
}

for item in data:
    if item['type'] == 'Province':
        province_dict.update({item['id']: item['name']})

for item in data:
    if item['type'] != 'Province':
        if item['parent'] in province_dict.keys():
            final_dict[province_dict[item['parent']]].update({item['name']: {}})


print final_dict

但是,似乎有些问题:

  1. 这不是实现这一目标的最有效的方法。
  2. 我不仅限于省市和都会区,也有区县等等。但是,它们都受相同的规则约束,每个孩子都有一个以省份为根的父母身份。
  3. 我需要创建一个上面提到的具有n个嵌套数量的分层结构。

如果有人可以帮助我实现这一目标,将会很有帮助。

1 个答案:

答案 0 :(得分:0)

我认为您可能想创建一个抽象数据类型来有效地做到这一点。您可以让ADT类使用字典,也可以给它另一个属性。

这只是伪编码。我现在没有时间创建一个功能齐全的类,但是我认为这会有所帮助。

class SomeClass:
    _parents = {}
    _children = {}

    def add_obj(self, obj):
        if obj.noParent:
            self._parents[obj.get_id] = obj

        else:
            self._children[obj.get_id] = obj

    def to_string(self, id):
        for v1 in (self._parents if id is None else self._parents.get(id)):
            if v1.get_id in self._parents.keys():
                print(self.to_string(v1.get_id()))
            for v2 in self._children.get(id):
                print(v2.stuff)

如果您仍在挣扎,今天晚些时候或明天我有时间的时候,我将回到这个答案,看看是否可以使它正常运行。