根据具有子字符串的特定值删除字典项
Removing dictionary items based on specific values having a sub-string
我有一个基于 Cisco 交换机命令 'show cdp neighbor detail' 输出的字典。该字典有 200 多个键值对。示例字典对如下。
{'device_id': 'Switch-Hostname1', 'ip_address': '1.1.1.1', 'platform': 'Cisco IP Phone', 'capabilities': 'Host Phone Two-port Mac Relay', 'local_port': 'GigabitEthernet1/1', 'remote_port': 'Port 1'}
{'device_id': 'Phone-Hostname2', 'ip_address': '2.2.2.2', 'platform': 'WS-C3750', 'capabilities': 'Switch IGMP', 'local_port': 'GigabitEthernet1/2', 'remote_port': 'FastEthernet1/0/48'}
{'device_id': 'AccessPoint-Hostname3', 'ip_address': '3.3.3.3', 'platform': 'AIR-CAP3700-K9', 'capabilities': 'Trans-Bridge Source-Route-Bridge IGMP', 'local_port': 'GigabitEthernet1/3', 'remote_port': 'GigabitEthernet0'}
我需要根据字典键 'platform' 的值删除字典项。 key 'platform'对应的值不应该包含上面示例代码中第三个条目中提到的子字符串"AIR-"。
请指教我怎样才能做到这一点,我正在使用 Python 3.
编辑
实际上我想删除在键 'platform' 的值中具有子字符串 "AIR-" 的字典。
并且,在第一次过滤之后,我想从剩余的字典中检索 'local_port' 的值,最好是在列表中。
您的问题有点不清楚,如果特定值有子字符串,您是要求只删除包含子字符串的项目还是整个字典?
在第一种情况下,您可以按如下方式使用字典理解:
orig = {'device_id': 'AccessPoint-Hostname3', 'ip_address': '3.3.3.3', 'platform': 'AIR-CAP3700-K9', 'capabilities': 'Trans-Bridge Source-Route-Bridge IGMP', 'local_port': 'GigabitEthernet1/3', 'remote_port': 'GigabitEthernet0'}
new = {k: v for k, v in orig.items() if substr not in v}
在第二种情况下,您可以使用列表理解:
info_old = [{'device_id': 'Switch-Hostname1', 'ip_address': '1.1.1.1', 'platform': 'Cisco IP Phone', 'capabilities': 'Host Phone Two-port Mac Relay', 'local_port': 'GigabitEthernet1/1', 'remote_port': 'Port 1'},
{'device_id': 'Phone-Hostname2', 'ip_address': '2.2.2.2', 'platform': 'WS-C3750', 'capabilities': 'Switch IGMP', 'local_port': 'GigabitEthernet1/2', 'remote_port': 'FastEthernet1/0/48'},
{'device_id': 'AccessPoint-Hostname3', 'ip_address': '3.3.3.3', 'platform': 'AIR- CAP3700-K9', 'capabilities': 'Trans-Bridge Source-Route-Bridge IGMP', 'local_port': 'GigabitEthernet1/3', 'remote_port': 'GigabitEthernet0'}]
info_new = [r for r in info_old if not substr in r[field]]
其中 substr
是您要避免的字符串 ("AIR-"
),而 field
是您正在查找的字段("platform"
在您的例子中)
编辑:
现在对问题进行了编辑,使其更加清晰。对于第一部分,给定的解决方案应该按预期工作:
info_new = [r for r in info_old if not substr in r[unwanted_field]]
对于第二部分,你可以做另一个列表理解:
ports = [r[wanted_field] for r in info_new]
或者,如果您不需要用于任何其他目的的已清理词典列表,您可以将其合并为一个推导式,如下所示:
ports = [r[wanted_field] for r in info_old if not substr in r[unwanted_field]]
其中info_old
是原始字典的列表,substr
要解析的部分("AIR-"
),wanted_field
字段名加上您要隔离的值 ("local_port"
) 和 unwanted_field
您要搜索的字段的名称 substr
("platform"
)
我认为在这种情况下最好的选择是使用 DataFrame。在你的字典中,你有相同的项目键。因此,您可以通过一种简单的方式更改为数据框,它可以让您轻松进入数据:
import pandas as pd
dic1 = {'device_id': 'Switch-Hostname1', 'ip_address': '1.1.1.1', 'platform': 'Cisco IP Phone', 'capabilities': 'Host Phone Two-port Mac Relay', 'local_port': 'GigabitEthernet1/1', 'remote_port': 'Port 1'}
dic2 = {'device_id': 'Phone-Hostname2', 'ip_address': '2.2.2.2', 'platform': 'WS-C3750', 'capabilities': 'Switch IGMP', 'local_port': 'GigabitEthernet1/2', 'remote_port': 'FastEthernet1/0/48'}
dic3 = {'device_id': 'AccessPoint-Hostname3', 'ip_address': '3.3.3.3', 'platform': 'AIR-CAP3700-K9', 'capabilities': 'Trans-Bridge Source-Route-Bridge IGMP', 'local_port': 'GigabitEthernet1/3', 'remote_port': 'GigabitEthernet0'}`
data = pd.DataFrame([dic1, dic2, dic3]);
data = data[data['platform'].str.contains("AIR") != True]
我有一个基于 Cisco 交换机命令 'show cdp neighbor detail' 输出的字典。该字典有 200 多个键值对。示例字典对如下。
{'device_id': 'Switch-Hostname1', 'ip_address': '1.1.1.1', 'platform': 'Cisco IP Phone', 'capabilities': 'Host Phone Two-port Mac Relay', 'local_port': 'GigabitEthernet1/1', 'remote_port': 'Port 1'}
{'device_id': 'Phone-Hostname2', 'ip_address': '2.2.2.2', 'platform': 'WS-C3750', 'capabilities': 'Switch IGMP', 'local_port': 'GigabitEthernet1/2', 'remote_port': 'FastEthernet1/0/48'}
{'device_id': 'AccessPoint-Hostname3', 'ip_address': '3.3.3.3', 'platform': 'AIR-CAP3700-K9', 'capabilities': 'Trans-Bridge Source-Route-Bridge IGMP', 'local_port': 'GigabitEthernet1/3', 'remote_port': 'GigabitEthernet0'}
我需要根据字典键 'platform' 的值删除字典项。 key 'platform'对应的值不应该包含上面示例代码中第三个条目中提到的子字符串"AIR-"。
请指教我怎样才能做到这一点,我正在使用 Python 3.
编辑 实际上我想删除在键 'platform' 的值中具有子字符串 "AIR-" 的字典。 并且,在第一次过滤之后,我想从剩余的字典中检索 'local_port' 的值,最好是在列表中。
您的问题有点不清楚,如果特定值有子字符串,您是要求只删除包含子字符串的项目还是整个字典?
在第一种情况下,您可以按如下方式使用字典理解:
orig = {'device_id': 'AccessPoint-Hostname3', 'ip_address': '3.3.3.3', 'platform': 'AIR-CAP3700-K9', 'capabilities': 'Trans-Bridge Source-Route-Bridge IGMP', 'local_port': 'GigabitEthernet1/3', 'remote_port': 'GigabitEthernet0'}
new = {k: v for k, v in orig.items() if substr not in v}
在第二种情况下,您可以使用列表理解:
info_old = [{'device_id': 'Switch-Hostname1', 'ip_address': '1.1.1.1', 'platform': 'Cisco IP Phone', 'capabilities': 'Host Phone Two-port Mac Relay', 'local_port': 'GigabitEthernet1/1', 'remote_port': 'Port 1'},
{'device_id': 'Phone-Hostname2', 'ip_address': '2.2.2.2', 'platform': 'WS-C3750', 'capabilities': 'Switch IGMP', 'local_port': 'GigabitEthernet1/2', 'remote_port': 'FastEthernet1/0/48'},
{'device_id': 'AccessPoint-Hostname3', 'ip_address': '3.3.3.3', 'platform': 'AIR- CAP3700-K9', 'capabilities': 'Trans-Bridge Source-Route-Bridge IGMP', 'local_port': 'GigabitEthernet1/3', 'remote_port': 'GigabitEthernet0'}]
info_new = [r for r in info_old if not substr in r[field]]
其中 substr
是您要避免的字符串 ("AIR-"
),而 field
是您正在查找的字段("platform"
在您的例子中)
编辑: 现在对问题进行了编辑,使其更加清晰。对于第一部分,给定的解决方案应该按预期工作:
info_new = [r for r in info_old if not substr in r[unwanted_field]]
对于第二部分,你可以做另一个列表理解:
ports = [r[wanted_field] for r in info_new]
或者,如果您不需要用于任何其他目的的已清理词典列表,您可以将其合并为一个推导式,如下所示:
ports = [r[wanted_field] for r in info_old if not substr in r[unwanted_field]]
其中info_old
是原始字典的列表,substr
要解析的部分("AIR-"
),wanted_field
字段名加上您要隔离的值 ("local_port"
) 和 unwanted_field
您要搜索的字段的名称 substr
("platform"
)
我认为在这种情况下最好的选择是使用 DataFrame。在你的字典中,你有相同的项目键。因此,您可以通过一种简单的方式更改为数据框,它可以让您轻松进入数据:
import pandas as pd
dic1 = {'device_id': 'Switch-Hostname1', 'ip_address': '1.1.1.1', 'platform': 'Cisco IP Phone', 'capabilities': 'Host Phone Two-port Mac Relay', 'local_port': 'GigabitEthernet1/1', 'remote_port': 'Port 1'}
dic2 = {'device_id': 'Phone-Hostname2', 'ip_address': '2.2.2.2', 'platform': 'WS-C3750', 'capabilities': 'Switch IGMP', 'local_port': 'GigabitEthernet1/2', 'remote_port': 'FastEthernet1/0/48'}
dic3 = {'device_id': 'AccessPoint-Hostname3', 'ip_address': '3.3.3.3', 'platform': 'AIR-CAP3700-K9', 'capabilities': 'Trans-Bridge Source-Route-Bridge IGMP', 'local_port': 'GigabitEthernet1/3', 'remote_port': 'GigabitEthernet0'}`
data = pd.DataFrame([dic1, dic2, dic3]);
data = data[data['platform'].str.contains("AIR") != True]