I have a dictionary in python that is statically initialized as shown below
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
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 = {
'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
.
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
.
Output as expected:
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.