Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/search/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python:从字典中提取键、值对-键包含特定文本_Python_Search_Dictionary_Count - Fatal编程技术网

Python:从字典中提取键、值对-键包含特定文本

Python:从字典中提取键、值对-键包含特定文本,python,search,dictionary,count,Python,Search,Dictionary,Count,我目前有一本字典如下所示: {OctetString('Ethernet8/6'): Integer(1), OctetString('Ethernet8/7'): Integer(2), OctetString('Ethernet8/8'): Integer(2), OctetString('Ethernet8/9'): Integer(1), OctetString('Vlan1'): Integer(2), OctetString('Vlan10'): Integer(1),

我目前有一本字典如下所示:

{OctetString('Ethernet8/6'): Integer(1),
 OctetString('Ethernet8/7'): Integer(2),
 OctetString('Ethernet8/8'): Integer(2),
 OctetString('Ethernet8/9'): Integer(1),
 OctetString('Vlan1'): Integer(2),
 OctetString('Vlan10'): Integer(1),
 OctetString('Vlan15'): Integer(1),
 OctetString('loopback0'): Integer(1),
 OctetString('mgmt0'): Integer(1),
 OctetString('port-channel1'): Integer(1),
 OctetString('port-channel10'): Integer(1),
 OctetString('port-channel101'): Integer(1),
 OctetString('port-channel102'): Integer(1)}
{OctetString('Ethernet8/6'): Integer(1),
 OctetString('Ethernet8/7'): Integer(2),
 OctetString('Ethernet8/8'): Integer(2),
 OctetString('Ethernet8/9'): Integer(1)}
我希望我的字典如下所示:

{OctetString('Ethernet8/6'): Integer(1),
 OctetString('Ethernet8/7'): Integer(2),
 OctetString('Ethernet8/8'): Integer(2),
 OctetString('Ethernet8/9'): Integer(1),
 OctetString('Vlan1'): Integer(2),
 OctetString('Vlan10'): Integer(1),
 OctetString('Vlan15'): Integer(1),
 OctetString('loopback0'): Integer(1),
 OctetString('mgmt0'): Integer(1),
 OctetString('port-channel1'): Integer(1),
 OctetString('port-channel10'): Integer(1),
 OctetString('port-channel101'): Integer(1),
 OctetString('port-channel102'): Integer(1)}
{OctetString('Ethernet8/6'): Integer(1),
 OctetString('Ethernet8/7'): Integer(2),
 OctetString('Ethernet8/8'): Integer(2),
 OctetString('Ethernet8/9'): Integer(1)}
我不确定找到这些键、值对的最佳方法是什么。我真的想要任何匹配“\Ethernet(\d*)/(\d*)”的东西。然而,我不确定最好的方法是什么。我的主要目标是匹配所有以太网值,然后对它们进行计数。例如:在我的dict匹配了所有Ethernetx/x之后,我想计算1和2的数量

还有,为什么我在迭代字典并打印时只得到Ethernet8/6,而在打印字典时得到的是OctetString('Ethernet8/6')

这应该做到:

new_dict = dict()
for key, value in orig_dict.items():
    if 'Ethernet' in str(key):
        new_dict[key] = value
当您使用
print
时,python在
OctetString
对象上调用
\uuuu str\uuuu
方法,该对象返回
Ethernet8/6
。但是,我认为
pprint
默认为打印对象类型

编辑:

Stefan Pochmann在下面正确地指出,中的
如果“Ethernet”与任何包含单词
Ethernet
的字符串匹配。OP确实在他的帖子中提到使用正则表达式来匹配
以太网(\d*)/(\d*)
,因此这个答案可能不适用于任何希望解决类似问题的人。

(我将在str(key)
测试中使用与公认答案相同的
'Ethernet'

如果您想保留原始dict并将过滤后的版本作为单独的词典,我会使用以下理解:

newdict = {key: value
           for key, value in mydict.items()
           if 'Ethernet' in str(key)}
如果不想保留原始dict,也可以删除不需要的条目:

for key in list(mydict):
    if 'Ethernet' in str(key):
        del mydict[key]
获取“OctetString(“…”)的原因与此相同:

>>> 'foo'
'foo'
>>> pprint.pprint('foo')
'foo'
>>> print('foo')
foo

前两个测试向您展示了可以在源代码中使用的表示,这就是为什么会有引号。这是
repr
函数为您提供的功能。第三个测试打印正常愉悦的值,因此不添加引号。“OctetString(“…”)就是这样一种表示,您可以将其复制并粘贴到源代码中,然后再次获取实际的OctetString对象,而不是Python字符串对象。我猜
pprint
主要用于开发,在开发中获得完整的
repr
版本更有用。

您几乎完成了,为
“\Ethernet(\d*)/(\d*)”
创建一个
regex
,并检查给定字典中的每个键。嘿@anmol\uppal,我对Python还是很陌生,我该怎么做呢?我在python中搜索过re,但在尝试让它工作时遇到了麻烦,re不喜欢在我迭代字典时使用它。我认为它需要一个字符串。在
re
模块中有很多方法,最适合您的是
re.match(expression,string)
,其中
expression='Ethernet(\d*)/(\d*)”
,string是您要测试的单词。如果单词与表达式不匹配,则返回None。什么是
OctetString('Ethernet8/6')
?解释下一票将很有帮助,而不仅仅是对我!它总是比我想象的要容易得多。。。谢谢。我经常有同样的问题@Matt!很高兴你成功了。@Erve1879不是我投了反对票,但我想这是因为你可能匹配的比你应该匹配的更多(例如“MyThernet”),或者是因为你让它变得比需要的更难。new_dict={k:v代表k,v在orig_dict.items()如果k中是“Ethernet”}谢谢你的解释,我使用pysnmp获取值,并将其转换为dict。我猜八进制字符串就是pysnmp解释数据的方式。有道理。