【问题标题】:Nested for loop issue always using index 0嵌套 for 循环问题始终使用索引 0
【发布时间】:2018-12-29 18:14:28
【问题描述】:

我的嵌套循环有问题。我有 ipaddress 和 hostname 的列表,地址循环是正确的,而在 hostname 上循环是不正确的,它总是在中断后使用索引 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 = 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**

谢谢

【问题讨论】:

  • 这个缩进是你原始代码的写法吗?你能在这里检查它是否正确吗?
  • 我看不出break 没有被执行。此外,您的循环可能应该是for j in hostname(不是nhostname,此处未定义)。
  • 另外,你真的想要一个嵌套循环吗?您可能想压缩两个列表并一起循环。
  • @ParitoshSingh,是的。那是原始的缩进。这是在不同论坛上发布的示例配置谢谢...python-forum.io/…
  • @busybear,在 j 的循环之后执行 break。还有一个打字错误..应该是nhostname(修改了帖子)。目前,我唯一知道的是嵌套循环,我不熟悉将列表循环在一起的 zip,但我会回顾一下。感谢分享

标签: python python-3.x loops for-loop network-programming


【解决方案1】:

您不需要嵌套循环:

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()

【讨论】:

  • 嗨@walid Da。 ,感谢您的回复。但结果是输入数字 0。这是打印输出: 现在访问设备:192.168.137.50 主机:192.168.137.50 主机名:0
  • 嗨@Walid Da.,再次感谢它现在正在工作。如果我的理解有误,请纠正我。从我们将列表放入 i 变量的更改中,该列表从范围 0 开始,直到 ipaddress 列表下的结尾/长度.....现在对于主机名,我们直接将其放入操作,即 tn.write...question在主机名中使用此命令 [i] 是否意味着它根据 i 链接的 IP 地址列表复制/使用索引?我们还会自动去掉主机名列表上的括号吗?谢谢
  • 其实 i 只是一个索引,可以让你访问两个数组中的元素。我们使用的是相同的,因为两个数组的长度相同。
  • 感谢您的帮助
【解决方案2】:

您在 nhostname 循环的末尾放置了一个 break 语句,这意味着嵌套循环在第一次迭代后中断并返回到 ipadress 循环。

删除嵌套循环末尾的 break 语句,它应该可以工作。

【讨论】:

  • 是的,我放了break..但它循环,直到它执行所有不是y目标的主机名列表......循环的顺序应该是ipaddress然后是主机名然后回到ipaddress。谢谢
  • 在这种情况下,您甚至不需要两个循环。只需创建一个循环遍历索引而不是列表中的项目或使用 zip 函数。
  • 复制。谢谢
【解决方案3】:

你需要类似的东西

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.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()

【讨论】:

  • 这也有效。很好.. 学到了一种新方法 :).. 在这个 zip 中,我们聚合了多个列表,这些列表将分配到单独的变量中。? ..................................................... .........另一个基本问题..应该是当我们打印列表时。在 for 循环之前 print(ipaddress) 我们看到带有括号和逗号的元素...确实将其分配给新变量 ex。我会将它转换为类似的字符串?谢谢
  • 谢谢@bitto,会审核的。
  • @searching1 一切顺利。
猜你喜欢
  • 2015-02-14
  • 2021-12-01
  • 1970-01-01
  • 2018-06-07
  • 1970-01-01
  • 2012-01-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多