-2
我有一個嵌套的dic,我需要循環它來檢查ip = x和cmd = y然後採取行動xyz,我該怎麼做?需要關於python嵌套字典的幫助
d={'ip': {'cmd': 'cmd_out'}, 'ip1': {'cmd1': 'cmd_out1'}}
我能夠得到如下:
for ip, cmd in d.items():
print ip,cmd
出來: -
ip {'cmd': 'cmd_out'}
ip1 {'cmd1': 'cmd_out1'}
我想要的是像下面,但不工作:
for ip, cmd in d.items():
if ip =='ip' and cmd=='cmd':
print 'first IP' , ip ### take action
elif ip=='ip1' and cmd='cmd1':
print "second ip" , ip #####take action
我對Python來說是新的,所以更簡單更好:)
請點擊這裏:http://stackoverflow.com/questions/3294889/iterating-over-dictionaries-using-for-loops-in-python – metmirr