Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/336.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
分配IP和x27的列表;s到Python中的开关列表_Python - Fatal编程技术网

分配IP和x27的列表;s到Python中的开关列表

分配IP和x27的列表;s到Python中的开关列表,python,Python,如何将交换机列表分配给一系列IP 例: 我希望能够将“交换机列表”中的第一个交换机分配给“IP列表”中的第一个IP。如果IP多于交换机,我不希望它为交换机分配多个IP。每个交换机只需要一个IP if len(ip) == len(list): return zip(ip, list) # Returns [("10.15.15.1", "switch1.com"), ...] 或者,如果希望其中一个为键,另一个为值: if len(ip) == len(list): ret =

如何将交换机列表分配给一系列IP

例:

我希望能够将“交换机列表”中的第一个交换机分配给“IP列表”中的第一个IP。如果IP多于交换机,我不希望它为交换机分配多个IP。每个交换机只需要一个IP

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

您想要以下内容吗

>>> 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条件中删除
return
语句:)在
if
条件中没有
return
,坦白地说,我从来没有见过任何人在
if
条件下返回。@avasal但在if条件中没有。如果OP使用你的代码,,他将在函数外得到一个语法错误
'return'
@avasal,该语句可能是正确的。是什么使它比
zip
更好?不,在这种情况下它不比zip好。刚刚更新,谢谢。这就是我要找的。我希望即使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')]