简体   繁体   中英

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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