简体   繁体   English

使用for-else循环时重复添加行

[英]Repeated addition of line when using for-else loop

I have a function 我有一个功能

def static_nat_fwsm_check(mapped_to_real_address_pairs,FWSM_ACLs):
  amended_static_nat_acl=[]
  for acl_line in FWSM_ACLs:
    for i,j in mapped_to_real_address_pairs:
      if j in acl_line:
        amended_static_nat_acl.append('!STATIC NAT SUBSTITUTION FOR '+acl_line)
        amended_static_nat_acl.append(re.sub(j,i,acl_line))
        break
    else:
      amended_static_nat_acl.append(acl_line)
  return amended_static_nat_acl

The idea behind the function is to take a list of tuples and a list of strings as arguments. 该函数背后的思想是采用元组列表和字符串列表作为参数。 The tuples contain a mapping of real addresses to mapped addresses. 元组包含实际地址到映射地址的映射。 If the mapped address is found in the string in the list, then it is replaced with the real address 如果在列表的字符串中找到映射的地址,则将其替换为真实地址

The last line in the list of strings is added twice to the new list I am creating. 字符串列表的最后一行被两次添加到我正在创建的新列表中。 When I step through the function adding print statements, it looks like the last tuple in the list is evaluated twice. 当我逐步执行添加打印语句的功能时,似乎列表中的最后一个元组被评估了两次。

I could sidestep the problem by adding another if statement to the else clause, but I'd like to understand where I'm going wrong. 我可以通过在else子句中添加另一个if语句来回避问题,但是我想了解我要去哪里。 Any assistance greatly appreciated! 任何帮助,不胜感激!

User error. 用户错误。 I used a previous function to generate input for this function. 我使用了以前的函数来为此函数生成输入。 Loosely defined regex, plus an ACL name that was a subset of other ACL names caused the duplicate output. 松散定义的正则表达式,加上作为其他ACL名称子集的ACL名称,导致重复输出。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM