python - 更改 New_dictionary 中键的名称,其中 New_dictionary 作为 Main_dictionary 的值

标签 python list dictionary

我有一本字典:(键是“名称”,值是其他字典)

dictionary = {'subnet 10.5.5.0 netmask 255.255.255.224': 
                         {'option-domain-name': '"internal.example.org"',
                         'option-domain-name-servers': 'ns1.internal.example.org',
                         'range': '10.5.5.26 10.5.5.30', 'max-lease-time': '7200', 
                         'default-lease-time': '600', 'option-routers': '10.5.5.1',
                         'option-broadcast-address': '10.5.5.31'},
              'subnet 10.254.239.32 netmask 255.255.255.224': 
                         {'range': 'dynamic-bootp 10.254.239.40 10.254.239.60',
                          'option-routers': 'rtr-239-32-1.example.org', 
                          'option-broadcast-address': '10.254.239.31'}, 
              'subnet 10.254.239.0 netmask 255.255.255.224': 
                         {'range': '10.254.239.10 10.254.239.20',
                          'option-routers': 'rtr-239-0-1.example.org, rtr-239-0-     
                           2.example.org'}, 
              'host fantasia': {'hardware': 'ethernet 08:00:07:26:c0:a5', 
                                'fixed-address': 'fantasia.fugue.com'}, 
              'host passacaglia': {'hardware': 'ethernet 0:0:c0:5d:bd:95',
                                   'server-name': '"toccata.fugue.com"',
                                   'filename': '"vmunix.passacaglia"'}
         }

我所要做的就是改变每一个 '选项-'代表'选项'

dictionary = {'subnet 10.5.5.0 netmask 255.255.255.224': {'option domain-name': '"internal.example.org"', 'option domain-name-servers': 'ns1.internal.example.org', 'range': '10.5.5.26 10.5.5.30', 'max-lease-time': '7200', 'default-lease-time': '600', 'option routers': '10.5.5.1', 'option broadcast-address': '10.5.5.31'}, 'subnet 10.254.239.32 netmask 255.255.255.224': {'range': 'dynamic-bootp 10.254.239.40 10.254.239.60', 'option routers': 'rtr-239-32-1.example.org', 'option broadcast-address': '10.254.239.31'}, 'subnet 10.254.239.0 netmask 255.255.255.224': {'range': '10.254.239.10 10.254.239.20', 'option routers': 'rtr-239-0-1.example.org, rtr-239-0-2.example.org'}, 'host fantasia': {'hardware': 'ethernet 08:00:07:26:c0:a5', 'fixed-address': 'fantasia.fugue.com'}, 'host passacaglia': {'hardware': 'ethernet 0:0:c0:5d:bd:95', 'server-name': '"toccata.fugue.com"', 'filename': '"vmunix.passacaglia"'}}

我该怎么做?

最佳答案

for subdict in dictionary.values():
    for key, val in subdict.items():
        if key.startswith('option-'):
            del subdict[key]
            subdict[' '.join(key.split('-', 1))] = val

因此,对于每个包含的字典,循环遍历其项目,如果键以 option- 开头,则从字典中删除该键,并将该值存储在删除破折号的新键下.

演示:

>>> from pprint import pprint
>>> dictionary = {'subnet 10.5.5.0 netmask 255.255.255.224': {'option-domain-name': '"internal.example.org"', 'option-domain-name-servers': 'ns1.internal.example.org', 'range': '10.5.5.26 10.5.5.30', 'max-lease-time': '7200', 'default-lease-time': '600', 'option-routers': '10.5.5.1', 'option-broadcast-address': '10.5.5.31'}, 'subnet 10.254.239.32 netmask 255.255.255.224': {'range': 'dynamic-bootp 10.254.239.40 10.254.239.60', 'option-routers': 'rtr-239-32-1.example.org', 'option-broadcast-address': '10.254.239.31'}, 'subnet 10.254.239.0 netmask 255.255.255.224': {'range': '10.254.239.10 10.254.239.20', 'option-routers': 'rtr-239-0-1.example.org, rtr-239-0-2.example.org'}, 'host fantasia': {'hardware': 'ethernet 08:00:07:26:c0:a5', 'fixed-address': 'fantasia.fugue.com'}, 'host passacaglia': {'hardware': 'ethernet 0:0:c0:5d:bd:95', 'server-name': '"toccata.fugue.com"', 'filename': '"vmunix.passacaglia"'}}
>>> for subdict in dictionary.values():
...     for key, val in subdict.items():
...         if key.startswith('option-'):
...             del subdict[key]
...             subdict[' '.join(key.split('-', 1))] = val
... 
>>> pprint(dictionary)
{'host fantasia': {'fixed-address': 'fantasia.fugue.com',
                   'hardware': 'ethernet 08:00:07:26:c0:a5'},
 'host passacaglia': {'filename': '"vmunix.passacaglia"',
                      'hardware': 'ethernet 0:0:c0:5d:bd:95',
                      'server-name': '"toccata.fugue.com"'},
 'subnet 10.254.239.0 netmask 255.255.255.224': {'option routers': 'rtr-239-0-1.example.org, rtr-239-0-2.example.org',
                                                 'range': '10.254.239.10 10.254.239.20'},
 'subnet 10.254.239.32 netmask 255.255.255.224': {'option broadcast-address': '10.254.239.31',
                                                  'option routers': 'rtr-239-32-1.example.org',
                                                  'range': 'dynamic-bootp 10.254.239.40 10.254.239.60'},
 'subnet 10.5.5.0 netmask 255.255.255.224': {'default-lease-time': '600',
                                             'max-lease-time': '7200',
                                             'option broadcast-address': '10.5.5.31',
                                             'option domain-name': '"internal.example.org"',
                                             'option domain-name-servers': 'ns1.internal.example.org',
                                             'option routers': '10.5.5.1',
                                             'range': '10.5.5.26 10.5.5.30'}}

关于python - 更改 New_dictionary 中键的名称,其中 New_dictionary 作为 Main_dictionary 的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11273504/

相关文章:

java - 列表转队列如何实现FIFO

c# - 使用字典创建对象的新实例

python - 是什么导致 numpy.sum 和 numpy.cumsum 之间的性能不同?

python - 有没有办法让这个逆阶乘代码更有效地运行

python - (Python 2.7) easygui.choicebox if else 语句如何

list - Haskell 中列表的 `==` 的源代码定义

python - 如何获取 geotiff 中单元格的坐标?

list - 通过函数组合有效的列表追加/前置

swift - 找不到 Swift 字典 "keys"属性的内部实现

swift - 使结构可哈希?