Python 嵌套for循环问题始终使用索引0
我的嵌套循环有问题。我有IP地址和主机名列表,地址循环正确,而主机名循环不正确,它总是在中断后使用索引0。Python 嵌套for循环问题始终使用索引0,python,python-3.x,loops,for-loop,network-programming,Python,Python 3.x,Loops,For Loop,Network Programming,我的嵌套循环有问题。我有IP地址和主机名列表,地址循环正确,而主机名循环不正确,它总是在中断后使用索引0。 # list example: ipaddress = ['192.168.1.1', '192.168.1.2'] nhostname = ['lab-sw01', 'lab-rtr02'] for i in ipaddress: print ("Now accessing the device: ", i) dev = i.strip() tn = telne
# list example:
ipaddress = ['192.168.1.1', '192.168.1.2']
nhostname = ['lab-sw01', 'lab-rtr02']
for i in ipaddress:
print ("Now accessing the device: ", i)
dev = i.strip()
tn = telnetlib.Telnet(dev)
print("Host: ",dev)
tn.read_until(b"Username:")
tn.write(user.encode("ascii") + b"\n")
for j in nhostname:
print ("Hostname :", j)
## hn = i.strip()
if password:
tn.read_until(b"Password: ")
tn.write(password.encode("ascii") + b"\n")
tn.write(b"conf t\r\n")
time.sleep(2)
tn.write(("hostname " + j + "\n").encode('ascii'))
time.sleep(2)
tn.write(b"exit \n")
tn.write(b"wr mem \n")
tn.close()
break
输出:
Now accessing the device: 192.168.137.50
Host: 192.168.137.50
Hostname **: lab-sw01**
Now accessing the device: 192.168.137.51
Host: 192.168.137.51
Hostname : **lab-sw01**
Now accessing the device: 192.168.1.1
Host: 192.168.1.1
hostname lab-sw01
Now accessing the device: 192.168.1.2
Host: 192.168.1.2
hostname lab-rtr02
谢谢您不需要嵌套循环:
for i in range(0,len(ipaddress)):
print ("Now accessing the device: ",ipaddress[i])
dev = ipaddress[i].strip()
tn = telnetlib.Telnet(dev)
print("Host: ",dev)
tn.read_until(b"Username:")
tn.write(user.encode("ascii") + b"\n")
print ("Hostname :", hostname[i])
if password:
tn.read_until(b"Password: ")
tn.write(password.encode("ascii") + b"\n")
tn.write(b"conf t\r\n")
time.sleep(2)
tn.write(("hostname " + hostname[i] + "\n").encode('ascii'))
time.sleep(2)
tn.write(b"exit \n")
tn.write(b"wr mem \n")
tn.close()
您不需要嵌套循环:
for i in range(0,len(ipaddress)):
print ("Now accessing the device: ",ipaddress[i])
dev = ipaddress[i].strip()
tn = telnetlib.Telnet(dev)
print("Host: ",dev)
tn.read_until(b"Username:")
tn.write(user.encode("ascii") + b"\n")
print ("Hostname :", hostname[i])
if password:
tn.read_until(b"Password: ")
tn.write(password.encode("ascii") + b"\n")
tn.write(b"conf t\r\n")
time.sleep(2)
tn.write(("hostname " + hostname[i] + "\n").encode('ascii'))
time.sleep(2)
tn.write(b"exit \n")
tn.write(b"wr mem \n")
tn.close()
在nhostname循环的末尾放一个break语句,这意味着嵌套循环在第一次迭代后中断,并返回到iPAddress循环
删除嵌套循环结尾处的break语句,它应该可以工作。在nhostname循环结尾处放置break语句,这意味着嵌套循环在第一次迭代后中断,并返回到iPAddress循环
删除嵌套循环末尾的break语句,它应该可以工作。您需要类似
ipaddress = ['192.168.1.1', '192.168.1.2']
nhostname = ['lab-sw01', 'lab-rtr02']
for i,j in zip(ipaddress,nhostname):
print ("Now accessing the device: ", i)
dev = i.strip()
print("Host: ",dev)
print("hostname " + j + "\n")
输出:
Now accessing the device: 192.168.137.50
Host: 192.168.137.50
Hostname **: lab-sw01**
Now accessing the device: 192.168.137.51
Host: 192.168.137.51
Hostname : **lab-sw01**
Now accessing the device: 192.168.1.1
Host: 192.168.1.1
hostname lab-sw01
Now accessing the device: 192.168.1.2
Host: 192.168.1.2
hostname lab-rtr02
您的内部循环正在中断,但在外部for循环的第二个循环中,它正在重新开始。这就是为什么你的主机名永远不会改变
ipaddress = ['192.168.1.1', '192.168.1.2']
nhostname = ['lab-sw01', 'lab-rtr02']
for i in ipaddress:
print ("Now accessing the device: ", i)
print("Host: ",dev)
for j in nhostname: #In every looping of the outer loop it will access the first element of the nhostname
print ("Hostname :", j)
break
我认为你的代码应该是-
ipaddress = ['192.168.1.1', '192.168.1.2']
nhostname = ['lab-sw01', 'lab-rtr02']
for i,j in zip(ipaddress,nhostname):
print ("Now accessing the device: ", i)
dev = i.strip()
tn = telnetlib.Telnet(dev)
print("Host: ",dev)
tn.read_until(b"Username:")
tn.write(user.encode("ascii") + b"\n")
print ("Hostname :", j)
## hn = i.strip()
if password:
tn.read_until(b"Password: ")
tn.write(password.encode("ascii") + b"\n")
tn.write(b"conf t\r\n")
time.sleep(2)
tn.write(("hostname " + j + "\n").encode('ascii'))
time.sleep(2)
tn.write(b"exit \n")
tn.write(b"wr mem \n")
tn.close()
你需要像这样的东西
ipaddress = ['192.168.1.1', '192.168.1.2']
nhostname = ['lab-sw01', 'lab-rtr02']
for i,j in zip(ipaddress,nhostname):
print ("Now accessing the device: ", i)
dev = i.strip()
print("Host: ",dev)
print("hostname " + j + "\n")
输出:
Now accessing the device: 192.168.137.50
Host: 192.168.137.50
Hostname **: lab-sw01**
Now accessing the device: 192.168.137.51
Host: 192.168.137.51
Hostname : **lab-sw01**
Now accessing the device: 192.168.1.1
Host: 192.168.1.1
hostname lab-sw01
Now accessing the device: 192.168.1.2
Host: 192.168.1.2
hostname lab-rtr02
您的内部循环正在中断,但在外部for循环的第二个循环中,它正在重新开始。这就是为什么你的主机名永远不会改变
ipaddress = ['192.168.1.1', '192.168.1.2']
nhostname = ['lab-sw01', 'lab-rtr02']
for i in ipaddress:
print ("Now accessing the device: ", i)
print("Host: ",dev)
for j in nhostname: #In every looping of the outer loop it will access the first element of the nhostname
print ("Hostname :", j)
break
我认为你的代码应该是-
ipaddress = ['192.168.1.1', '192.168.1.2']
nhostname = ['lab-sw01', 'lab-rtr02']
for i,j in zip(ipaddress,nhostname):
print ("Now accessing the device: ", i)
dev = i.strip()
tn = telnetlib.Telnet(dev)
print("Host: ",dev)
tn.read_until(b"Username:")
tn.write(user.encode("ascii") + b"\n")
print ("Hostname :", j)
## hn = i.strip()
if password:
tn.read_until(b"Password: ")
tn.write(password.encode("ascii") + b"\n")
tn.write(b"conf t\r\n")
time.sleep(2)
tn.write(("hostname " + j + "\n").encode('ascii'))
time.sleep(2)
tn.write(b"exit \n")
tn.write(b"wr mem \n")
tn.close()
这是原始代码的编写方式吗?你能在这里检查一下它是否正确吗?我看不出有任何方法不执行
break
。另外,您的循环可能应该是hostname中j的(而不是nhostname
,这里没有定义)。此外,您真的想要嵌套循环吗?您可能想将两个列表压缩并循环在一起。@Paritossingh,是的。这是原始缩进。这是在不同论坛上发布的示例配置,谢谢…@busybear,在j的循环之后执行break。还有一个输入错误..应该是nhostname(修改了帖子)。现在,我只知道嵌套循环,我不熟悉将列表循环在一起的zip,但我会回顾一下。谢谢分享这个缩进是怎么写你的原始代码的?你能在这里检查一下它是否正确吗?我看不出有任何方法不执行break
。另外,您的循环可能应该是hostname
中j的(而不是nhostname
,这里没有定义)。此外,您真的想要嵌套循环吗?您可能想将两个列表压缩并循环在一起。@Paritossingh,是的。这是原始缩进。这是在不同论坛上发布的示例配置,谢谢…@busybear,在j的循环之后执行break。还有一个输入错误..应该是nhostname(修改了帖子)。现在,我只知道嵌套循环,我不熟悉将列表循环在一起的zip,但我会回顾一下。谢谢你在瓦利达分享,谢谢你的回复。但结果是输入数字0。这是打印输出:正在访问设备:192.168.137.50主机:192.168.137.50主机名:0Hi@Walid Da.,再次感谢它现在工作。如果我的理解有误,请纠正我。从我们将列表更改为i变量开始,该列表从范围0开始,直到ipaddress list下的结束/长度。。。。。现在,对于主机名,我们直接将其放入操作tn.write…问题:主机名中的这个命令[i]是否意味着它复制/使用基于i的索引,而i是链接的IP地址列表?我们还会自动删除主机名列表中的括号吗?感谢i实际上只是一个索引,允许您访问两个数组中的元素。我们使用相同的数组,因为两个数组的长度相同。感谢您的帮助hi@walid Da,谢谢你的回复。但结果是输入数字0。这是打印输出:正在访问设备:192.168.137.50主机:192.168.137.50主机名:0Hi@Walid Da.,再次感谢它现在工作。如果我的理解有误,请纠正我。从我们将列表更改为i变量开始,该列表从范围0开始,直到ipaddress list下的结束/长度。。。。。现在,对于主机名,我们直接将其放入操作tn.write…问题:主机名中的这个命令[i]是否意味着它复制/使用基于i的索引,而i是链接的IP地址列表?我们还会自动删除主机名列表中的括号吗?感谢i实际上只是一个索引,允许您访问两个数组中的元素。我们使用的是相同的,因为两个数组的长度相同。谢谢你的帮助是的,我把中断。。但它会循环,直到它执行所有的主机名列表,而不是y目标…循环的顺序应该是ipaddress,然后是hostname,然后返回ipaddress。谢谢在这种情况下你甚至不需要两个循环。只需为循环创建一个for,循环遍历索引而不是列表中的项,或者使用zip函数.copy。谢谢你是的,我把休息时间给你了。。但它会循环,直到它执行所有的主机名列表,而不是y目标…循环的顺序应该是ipaddress,然后是hostname,然后返回ipaddress。谢谢在这种情况下你甚至不需要两个循环。只需为循环创建一个for,循环遍历索引而不是列表中的项,或者使用zip函数.copy。谢谢你,也谢谢你的作品。美好的学会了一种新方法:)。。在此zip中,我们汇总了将在单独变量中分配的多个列表。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。另一个基本问题。。当我们在for循环之前打印列表例如print(ipaddress)时,我们会看到带括号和逗号的元素。。。是否将其分配给新变量B