简体   繁体   English

从字典访问一组值

[英]accessing a set of values from a dictionary

I have a dictionary in python that is statically initialized as shown below 我在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'
               };

How do I get the mac-addresses alone inside a loop.Basically I want to be able to print out 我如何在循环中单独获得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

How do I do this. 我该怎么做呢。

Iterate over switch_port_to_host , it'll give you the key then print the value of the key 遍历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]

Use: 采用:

 switch_port_to_host.values()

To get the set of values as a list . list获取一组值。

You can print them on separate lines using: 您可以使用以下命令在单独的行上打印它们:

for x in switch_port_to_host.values():
    print x
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() gets your the values of your dictionary called dict . values()获取名为dict的字典的值。

Output as expected: 预期输出

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM