import netifaces as ni
temp=ni.interfaces()
print ni.ifaddresses(temp[0])
for a in ni.ifaddresses(temp[0]).iteritems():
print a
这在下面给我看:
{-1000: [{'addr': '24:f5:aa:30:ff:ff'}], 2: [{'broadcast': '11.4.9.255', 'netmask': '255.255.255.0', 'addr': '11.4.9.22'}], 23: [{'addr': 'fe80::34cc:afcf:c9d6:e25c%11'}]}
(-1000, [{'addr': '24:f5:aa:30:ff:ff'}])
(2, [{'broadcast': '11.4.9.255', 'netmask': '255.255.255.0', 'addr': '11.4.9.22'}])
(23, [{'addr': 'fe80::34cc:afcf:c9d6:e25c%11'}])
但我想只获得像24这样的地址值:f5:aa:30:ff:ff
如何使用python获取列表中的特定值?
答案 0 :(得分:0)
我能做的最好。
aux=[{-1000: [{'addr': '24:f5:aa:30:ff:ff'}], 2: [{'broadcast': '11.4.9.255', 'netmask': '255.255.255.0', 'addr': '11.4.9.22'}], 23: [{'addr':'fe80::34cc:afcf:c9d6:e25c%11'}]},
(-1000, [{'addr': '24:f5:aa:30:ff:ff'}]),
(2, [{'broadcast': '11.4.9.255', 'netmask': '255.255.255.0', 'addr': '11.4.9.22'}]),
(23, [{'addr': 'fe80::34cc:afcf:c9d6:e25c%11'}])]
import collections
def my_func(the_val):
if not isinstance(the_val, collections.Iterable) or isinstance(the_val, str):
return
if isinstance(the_val, dict) and the_val.has_key('addr'):
print "------------------->", the_val['addr']
for e in the_val:
if isinstance(the_val, dict):
my_func(the_val[e])
continue
my_func(e)
my_func(aux)