[英]Assign list of IP's to a list of switches in Python
我如何才能将交换机列表分配给一系列 IP?
前任:
list = [switch1.com, switch2.com, switch3.com, ...]
ip = [10.15.15.1, 10.15.15.2, all the way to 10.15.15.254]
我希望能够将“交换机列表”中的第一个交换机分配给“IP 列表”中的第一个 IP。 如果 IP 多于交换机,我不希望它为交换机分配多个 IP。 每个交换机只需要一个 IP。
您想要以下内容吗?
>>> sws= ['switch1','switch2','switch3']
>>> ips = ['10.15.15.1','10.15.15.2','10.15.15.3','10.15.15.4']
>>> print zip(sws,ips)
[('switch1', '10.15.15.1'), ('switch2', '10.15.15.2'),('switch3', '10.15.15.3')]
if len(ip) == len(list):
return zip(ip, list) # Returns [("10.15.15.1", "switch1.com"), ...]
或者,如果您希望一个是键,另一个是值:
if len(ip) == len(list):
ret = {}
for i, j in zip(ip, list):
ret[i] = j
return ret
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.