search - Python: Extract Key, Value Pairs From A Dictionary - Key Contains Specific Text -
i have dictionary looks this:
{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)} i want dictionary this:
{octetstring('ethernet8/6'): integer(1), octetstring('ethernet8/7'): integer(2), octetstring('ethernet8/8'): integer(2), octetstring('ethernet8/9'): integer(1)} i not sure best way find these key, value pairs. want matches '\ethernet(\d*)/(\d*)'. not sure best way go this. main goal match ethernet values , count them. example: after have dict matching of ethernetx/x want count amount of 1's , 2's.
also, why ethernet8/6 when iterate dictionary , print, when pprint dictionary end octetstring('ethernet8/6')?
for k in snmp_comb: print k ethernet2/18 ethernet2/31 ethernet2/30 ethernet2/32 ethernet8/46
this should it:
new_dict = dict() key, value in orig_dict.items(): if 'ethernet' in str(key): new_dict[key] = value when use print, python calls __str__ method on octetstring object, returns ethernet8/6. however, think pprint defaults printing object type.
edit:
stefan pochmann has rightly pointed out below if 'ethernet' in match string contains word ethernet. op did mention using regex in post match ethernet(\d*)/(\d*), answer may not suitable else looking solve similar problem.
Comments
Post a Comment