我在python中有一个静态初始化的字典,如下所示
switch_port_to_host = {
's1-eth1':'00:00:00:00:00:01',
's4-eth1':'00:00:00:00:00:02',
's2-eth1':'00:00:00:00:00:02',
's5-eth1':'00:00:00:00:00:05',
's3-eth1':'00:00:00:00:00:03',
's6-eth1':'00:00:00:00:00:06'
};
如何在循环中单独获取mac地址。基本上我希望能够打印出来
00:00:00:00:00:01
00:00:00:00:00:02
00:00:00:00:00:03
00:00:00:00:00:04
00:00:00:00:00:05
00:00:00:00:00:06
我该怎么做。
答案 0 :(得分:1)
迭代switch_port_to_host
,它会给你密钥,然后打印密钥的值
switch_port_to_host = {
's1-eth1':'00:00:00:00:00:01',
's4-eth1':'00:00:00:00:00:02',
's2-eth1':'00:00:00:00:00:02',
's5-eth1':'00:00:00:00:00:05',
's3-eth1':'00:00:00:00:00:03',
's6-eth1':'00:00:00:00:00:06'
};
for i in switch_port_to_host:
print switch_port_to_host[i]
答案 1 :(得分:1)
使用:
switch_port_to_host.values()
将值集合设为list
。
您可以使用以下方法在不同的行上打印:
for x in switch_port_to_host.values():
print x
答案 2 :(得分:1)
dict = {
's1-eth1':'00:00:00:00:00:01',
's4-eth1':'00:00:00:00:00:02',
's2-eth1':'00:00:00:00:00:02',
's5-eth1':'00:00:00:00:00:05',
's3-eth1':'00:00:00:00:00:03',
's6-eth1':'00:00:00:00:00:06'
}
# get values
print dict.values()
#by lines
for value in dict.values():
print value
values()
获取您的词典dict
的值。
输出: