Python嵌套字典函数重构

2024-10-05 20:05:49 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个字典,它包含一堆键/值对,其中一些键是字符串,另一些是更多的字典,等等。在

下面是我正在使用的一个示例词典:

{'1': {'0': {'hl': '', 'object_id': '590', 'state': 'T'},
       '1': {'hl': '', 'object_id': '590', 'state': 'T'},
       '2': {'hl': '', 'object_id': '590', 'state': 'T'}},
 '10': {'0': {'hl': '', 'object_id': '1194', 'state': 'T'},
        '1': {'hl': '', 'object_id': '1194', 'state': 'T'},
        '2': {'hl': '', 'object_id': '1194', 'state': 'T'}},
 '11': {'0': {'hl': '', 'object_id': '1195', 'state': 'T'},
        '1': {'hl': '', 'object_id': '1195', 'state': 'T'},
        '2': {'hl': '', 'object_id': '1195', 'state': 'T'}},
 '12': {'0': {'hl': '', 'object_id': '1196', 'state': 'T'},
        '1': {'hl': '', 'object_id': '1196', 'state': 'T'},
        '2': {'hl': '', 'object_id': '1196', 'state': 'T'}},
 '13': {'0': {'hl': '', 'object_id': '1197', 'state': 'T'},
        '1': {'hl': '', 'object_id': '1197', 'state': 'T'},
        '2': {'hl': '', 'object_id': '1197', 'state': 'T'}},
 '14': {'0': {'hl': '', 'object_id': '1198', 'state': 'T'},
        '1': {'hl': '', 'object_id': '1198', 'state': 'T'},
        '2': {'hl': '', 'object_id': '1198', 'state': 'T'}},
 '15': {'0': {'hl': '', 'object_id': '1199', 'state': 'T'},
        '1': {'hl': '', 'object_id': '1199', 'state': 'T'},
        '2': {'hl': '', 'object_id': '1199', 'state': 'T'}},
 '16': {'0': {'hl': '', 'object_id': '1200', 'state': 'T'},
        '1': {'hl': '', 'object_id': '1200', 'state': 'T'},
        '2': {'hl': '', 'object_id': '1200', 'state': 'T'}},
 '17': {'0': {'hl': '', 'object_id': '1201', 'state': 'T'},
        '1': {'hl': '', 'object_id': '1201', 'state': 'T'},
        '2': {'hl': '', 'object_id': '1201', 'state': 'T'}},
 '18': {'0': {'hl': '', 'object_id': '1202', 'state': 'T'},
        '1': {'hl': '', 'object_id': '1202', 'state': 'T'},
        '2': {'hl': '', 'object_id': '1202', 'state': 'T'}},
 '19': {'0': {'hl': '', 'object_id': '554', 'state': 'T'},
        '1': {'hl': '', 'object_id': '554', 'state': 'T'},
        '2': {'hl': '', 'object_id': '554', 'state': 'T'}},
 '2': {'0': {'hl': '', 'object_id': '590', 'state': 'T'},
       '1': {'hl': '', 'object_id': '590', 'state': 'T'},
       '2': {'hl': '', 'object_id': '590', 'state': 'T'}},
 '20': {'0': {'hl': '', 'object_id': '554', 'state': 'T'},
        '1': {'hl': '', 'object_id': '554', 'state': 'T'},
        '2': {'hl': '', 'object_id': '554', 'state': 'T'}},
 '21': {'0': {'hl': '', 'object_id': '949', 'state': 'T'},
        '1': {'hl': '', 'object_id': '949', 'state': 'T'},
        '2': {'hl': '', 'object_id': '949', 'state': 'T'}},
 '22': {'0': {'hl': '', 'object_id': '954', 'state': 'T'},
        '1': {'hl': '', 'object_id': '954', 'state': 'T'},
        '2': {'hl': '', 'object_id': '954', 'state': 'T'}},
 '23': {'0': {'hl': '', 'object_id': '554', 'state': 'T'},
        '1': {'hl': '', 'object_id': '554', 'state': 'T'},
        '2': {'hl': '', 'object_id': '554', 'state': 'T'}},
 '24': {'0': {'hl': '', 'object_id': '554', 'state': 'T'},
        '1': {'hl': '', 'object_id': '554', 'state': 'T'},
        '2': {'hl': '', 'object_id': '554', 'state': 'T'}},
 '25': {'0': {'hl': '', 'object_id': '1203', 'state': 'T'},
        '1': {'hl': '', 'object_id': '1203', 'state': 'T'},
        '2': {'hl': '', 'object_id': '1203', 'state': 'T'}},
 '26': {'0': {'hl': '', 'object_id': '1204', 'state': 'T'},
        '1': {'hl': '', 'object_id': '1204', 'state': 'T'},
        '2': {'hl': '', 'object_id': '1204', 'state': 'T'}},
 '27': {'0': {'hl': '', 'object_id': '1220', 'state': 'T'},
        '1': {'hl': '', 'object_id': '1220', 'state': 'T'},
        '2': {'hl': '', 'object_id': '1220', 'state': 'T'}},
 '28': {'0': {'hl': '', 'object_id': '1221', 'state': 'T'},
        '1': {'hl': '', 'object_id': '1221', 'state': 'T'},
        '2': {'hl': '', 'object_id': '1221', 'state': 'T'}},
 '29': {'0': {'hl': '', 'object_id': '1222', 'state': 'T'},
        '1': {'hl': '', 'object_id': '1222', 'state': 'T'},
        '2': {'hl': '', 'object_id': '1222', 'state': 'T'}},
 '3': {'0': {'hl': '', 'object_id': '590', 'state': 'T'},
       '1': {'hl': '', 'object_id': '590', 'state': 'T'},
       '2': {'hl': '', 'object_id': '590', 'state': 'T'}},
 '30': {'0': {'hl': '', 'object_id': '1223', 'state': 'T'},
        '1': {'hl': '', 'object_id': '1223', 'state': 'T'},
        '2': {'hl': '', 'object_id': '1223', 'state': 'T'}},
 '31': {'0': {'hl': '', 'object_id': '1224', 'state': 'T'},
        '1': {'hl': '', 'object_id': '1224', 'state': 'T'},
        '2': {'hl': '', 'object_id': '1224', 'state': 'T'}},
 '32': {'0': {'state': 'F'}, '1': {'state': 'F'}, '2': {'state': 'F'}},
 '33': {'0': {'state': 'F'}, '1': {'state': 'F'}, '2': {'state': 'F'}},
 '34': {'0': {'state': 'F'}, '1': {'state': 'F'}, '2': {'state': 'F'}},
 '35': {'0': {'state': 'F'}, '1': {'state': 'F'}, '2': {'state': 'F'}},
 '36': {'0': {'state': 'F'}, '1': {'state': 'F'}, '2': {'state': 'F'}},
 '37': {'0': {'state': 'F'}, '1': {'state': 'F'}, '2': {'state': 'F'}},
 '38': {'0': {'state': 'F'}, '1': {'state': 'F'}, '2': {'state': 'F'}},
 '39': {'0': {'state': 'F'}, '1': {'state': 'F'}, '2': {'state': 'F'}},
 '4': {'0': {'hl': '', 'object_id': '590', 'state': 'T'},
       '1': {'hl': '', 'object_id': '590', 'state': 'T'},
       '2': {'hl': '', 'object_id': '590', 'state': 'T'}},
 '40': {'0': {'state': 'F'}, '1': {'state': 'F'}, '2': {'state': 'F'}},
 '41': {'0': {'state': 'F'}, '1': {'state': 'F'}, '2': {'state': 'F'}},
 '42': {'0': {'state': 'F'}, '1': {'state': 'F'}, '2': {'state': 'F'}},
 '43': {'0': {'state': 'F'}, '1': {'state': 'F'}, '2': {'state': 'F'}},
 '44': {'0': {'state': 'F'}, '1': {'state': 'F'}, '2': {'state': 'F'}},
 '5': {'0': {'hl': '', 'object_id': '1010', 'state': 'T'},
       '1': {'hl': '', 'object_id': '1010', 'state': 'T'},
       '2': {'hl': '', 'object_id': '1010', 'state': 'T'}},
 '6': {'0': {'hl': '', 'object_id': '1010', 'state': 'T'},
       '1': {'hl': '', 'object_id': '1010', 'state': 'T'},
       '2': {'hl': '', 'object_id': '1010', 'state': 'T'}},
 '7': {'0': {'hl': '', 'object_id': '1010', 'state': 'T'},
       '1': {'hl': '', 'object_id': '1010', 'state': 'T'},
       '2': {'hl': '', 'object_id': '1010', 'state': 'T'}},
 '8': {'0': {'hl': '', 'object_id': '1010', 'state': 'T'},
       '1': {'hl': '', 'object_id': '1010', 'state': 'T'},
       '2': {'hl': '', 'object_id': '1010', 'state': 'T'}},
 '9': {'0': {'hl': '', 'object_id': '1193', 'state': 'T'},
       '1': {'hl': '', 'object_id': '1193', 'state': 'T'},
       '2': {'hl': '', 'object_id': '1193', 'state': 'T'}},
 'asset_no': '',
 'atags': {'0': {'tag': '$rackid_662'}, '1': {'tag': '$any_rack'}},
 'comment': '',
 'etags': {'21': {'id': '21',
                  'parent_id': '15',
                  'tag': 'DevIT',
                  'time': '1395166273',
                  'user': 'towens-admin'},
           '23': {'id': '23',
                  'parent_id': '9',
                  'tag': 'Internap SEF003',
                  'time': '1395166273',
                  'user': 'towens-admin'}},
 'has_problems': 'no',
 'height': '44',
 'id': '662',
 'isDeletable': '',
 'itags': {'15': {'id': '15',
                  'is_assignable': 'no',
                  'parent_id': '',
                  'refcnt': {'total': '0'},
                  'tag': 'IT Owner',
                  'trace': {}},
           '9': {'id': '9',
                 'is_assignable': 'no',
                 'parent_id': '',
                 'refcnt': {'total': '0'},
                 'tag': 'Site',
                 'trace': {}}},
 'location_id': '374',
 'location_name': 'Internap, SEF003',
 'mountedObjects': {'0': '590',
                    '1': '1010',
                    '10': '1201',
                    '11': '1202',
                    '12': '554',
                    '13': '949',
                    '14': '954',
                    '15': '1203',
                    '16': '1204',
                    '17': '1220',
                    '18': '1221',
                    '19': '1222',
                    '2': '1193',
                    '20': '1223',
                    '21': '1224',
                    '3': '1194',
                    '4': '1195',
                    '5': '1196',
                    '6': '1197',
                    '7': '1198',
                    '8': '1199',
                    '9': '1200'},
 'name': '13',
 'realm': 'rack',
 'row_id': '375',
 'row_name': 'B02-J'}

我需要在以下模式中提取第一个密钥:

^{pr2}$

我的代码是:

def get_RU(self, hostname):
        host_id = unicode(self.get_id(hostname))
        cab_no = int(self.depot[host_id]['rack_id'])
        cabinet = self.admin.get_rack(cab_no)
        cabinet_name = cabinet['name']
        for key in cabinet.keys():
            if type(cabinet[key]) == dict:
                for k in cabinet[key].keys():
                    if k == '0' and cabinet[key][k]['object_id'] == host_id:
                        return key, cabinet_name

我想做的是要么使用递归,要么找出一种方法来重构代码,这样代码就更干净了。任何帮助都将不胜感激。在


Tags: keyno代码nameselfidgetobject
1条回答
网友
1楼 · 发布于 2024-10-05 20:05:49

结构相当困难。我希望,我完全理解你的问题。在

需求评审

您的数据隐藏了主机的标识(您的key),因此您的搜索结果是:

hostname->;host_id->;(cabinet数据结构)[?key?]在

而拥有适当的key的条件是(keydata)[“0”][“object_id”]==host_id

什么可以重写

不为具有已知密钥的项循环

您可以简化:

for k in cabinet[key].keys():
   if k == '0' and cabinet[key][k]['object_id'] == host_id:
      pass #do something

收件人:

^{pr2}$

值类型测试

你的价值类型:

type(val) == dict

可以重写为:

^{4}$

在字典中循环键

for key in dct.keys():
    print key

通常写为:

for key in dct:
    print key

跳过dict类型的测试

这取决于您的数据是否正确。如果你不能依靠它,忽略它。在

重写函数

原来的功能如下:

def get_RU(self, hostname):
        host_id = unicode(self.get_id(hostname))
        cab_no = int(self.depot[host_id]['rack_id'])
        cabinet = self.admin.get_rack(cab_no)
        cabinet_name = cabinet['name']
        for key in cabinet.keys():
            if type(cabinet[key]) == dict:
                for k in cabinet[key].keys():
                    if k == '0' and cabinet[key][k]['object_id'] == host_id:
                        return key, cabinet_name

新版本:

def get_RU(self, hostname):
    host_id = unicode(self.get_id(hostname))
    cab_no = int(self.depot[host_id]['rack_id'])
    cabinet = self.admin.get_rack(cab_no)
    cabinet_name = cabinet['name']
    for key in cabinet:
        if cabinet[key]['0']['object_id'] == host_id:
            return key, cabinet_name

如果您确实无法防止键值不是dict的问题,可以添加try/catch

def get_RU(self, hostname):
    host_id = unicode(self.get_id(hostname))
    cab_no = int(self.depot[host_id]['rack_id'])
    cabinet = self.admin.get_rack(cab_no)
    cabinet_name = cabinet['name']
    for key in cabinet:
        try:
            if cabinet[key]['0']['object_id'] == host_id:
                return key, cabinet_name
        except (KeyError, TypeError):
            continue

更正数据结构

你应该重新组织你的数据,至少以这样一种方式,相同类型的结构生活在一起,不与其他结构混合。这样你就应该将键“0”、“1”、“2”等的所有值与文本值分开。这将使您不需要对dict类型的测试,并且还将使您的代码和数据更易于阅读。在

结论

丑陋的数据结构使代码变得困难。如果可以的话,请尝试纠正数据结构,理想情况下将其转换为dict,其中的键将是host_id,并为您今天搜索的key赋值。在

相关问题 更多 >