list append无法正常工作

时间:2013-12-10 22:23:26

标签: python list python-3.x dictionary append

我有代码应该填写IP地址范围,以便没有重叠。我有一个以前版本的这个工作很好,但我改变它自动化所有通行证,并减少浪费和资源密集。

这是我目前的代码:

import ipaddress
import csv
from csv import DictReader, DictWriter
import cProfile
import pstats
import operator
import time

start_time=time.strftime("%I:%M:%S %p", time.localtime())


alphlst=[]
SubnetDic1=[]
global SubnetDic2

with open(r'C:\ExampleBefore.csv', newline='') as fin:

    read = DictReader(fin)

    for line in read:
        alphlst.append(line)



def program():
    print("started")
    global alphlst
    lst3=[]
    global lst4
    lst4=[]
    global lst5
    lst5=[]
    endlist=[]
    global SubnetDic1
    SubnetDic1=[]
    global SubnetDic2
    SubnetDic2=[]
    global SubOut1
    SubOut1=[]
    global SubOut2
    SubOut2=[]
    msklst=[]
    global lst
    lst=[]

    print(alphlst)

    print("^ alphlst")


    for li in alphlst:
        li['CIDR']=ipaddress.ip_network(li['CIDR'])
        SubnetDic2.append(li)
        if str(li['CIDR'].netmask) != '255.255.255.255':
            SubnetDic1.append(li)

    print("alph list clear")


    alphlst=[]

    print("after alph list clear")


    print("Dic lists completed")

    def Overlap(y, z):

        global lst5
        global lst

        try:
            if y['CIDR'].overlaps(z['CIDR']):
                lst=list(y['CIDR'].address_exclude(z['CIDR'])) # List of subnets excluding line4 subnet
                if len(lst) != 0:
                    print("not empty. {} is obsolete.".format(y['CIDR']))
                    lst5.append(str(y['CIDR']))
                    print(lst)
        except ValueError:
            return False





    for line3 in SubnetDic1:
        line3['CIDR']=ipaddress.ip_network(line3['CIDR']) # Convert IP ranges to ip network object
        lst3.append(str(line3['CIDR']))
        for line4 in SubnetDic2:
            line4['CIDR']=ipaddress.ip_network(line4['CIDR']) # Convert IP ranges to ip network object
            Overlap(line3, line4) # Tests for IP overlap
            for val in lst:
                if str(val) not in lst3:
                    if str(val) not in lst4:
                        subwr=line3
                        subwr['CIDR']=val # New range
                        subwr['IPStart']=val.network_address #New net address
                        subwr['IPStop']=val.broadcast_address #New broadcast address
                        SubOut1.append(subwr)
                        print("{} is line3".format(subwr))
                        print("{} is line3 tst".format(line3))
                        lst4.append(str(val))



    print(SubOut1)
    print("^ SubOut1")




    print("Subnet part1 completed")

    global lst7


    lst7=[]


    for line5 in SubnetDic2:
        line5['CIDR']=ipaddress.ip_network(line5['CIDR']) # Convert IP ranges to ip network object
        lst7.append(str(line5['CIDR']))
        print("{} line5 end".format(line5))
        if str(line5['CIDR']) not in lst5:
            print("{} line5 aft".format(line5))
            SubOut2.append(line5) # Write lines

    print("Subnet part2 completed")


    for line6 in SubOut2:
        endlist.append(line6)

    print("Subnet part3 completed")

    for line7 in SubOut1:
        endlist.append(line7)

    print("Subnet part4 completed")

    lst8=[]

    for line10 in endlist:
        line10['CIDR']=ipaddress.ip_network(line10['CIDR'])
        line10['Mask']=line10['CIDR'].netmask
        print("{} is line10 try".format(line10))
        if str(line10['CIDR']) not in lst8:
            msklst.append(line10)
            lst8.append(str(line10['CIDR']))
            print("{} is line10 append".format(line10))



    srtlst=sorted(msklst, key=operator.itemgetter('Mask'), reverse=True)

    for lip in srtlst:
        alphlst.append(lip)






def all_true(items):
    return all(x == "True" for x in items)

lstadd=[]

def in_lst():

    global lst4
    global SubnetDic2
    global lstadd
    global lst7

    lstadd=[]

    for l in lst4:
        if l in lst7:
            lstadd.append("True")
        else:
            lstadd.append("False")

    print(lstadd)
    print(lst4)

def chk_lst():

    print("running chk_lst")

    in_lst()

    global lstadd

    if all_true(lstadd):
        return True
    else:
        return False


pass_start=time.strftime("%I:%M:%S %p", time.localtime())
cnt=1
cProfile.run('program()', 'prostats')
pass_end=time.strftime("%I:%M:%S %p", time.localtime())
p = pstats.Stats('prostats')
p.print_stats(0)
print("Pass {0}. Start: {1} End: {2}".format(cnt, pass_start, pass_end))


while chk_lst() is False:
    pass_start2=time.strftime("%I:%M:%S %p", time.localtime())
    cnt = cnt + 1
    cProfile.run('program()', 'prostats2')
    pass_end2=time.strftime("%I:%M:%S %p", time.localtime())
    p = pstats.Stats('prostats2')
    p.print_stats(0)
    print("Pass {0}. Start: {1} End: {2}".format(cnt, pass_start2, pass_end2))


print("All Subnets Done")    


with open(r'C:\ExampleBefore.csv', newline='') as fin7,\
     open(r'C:\TstExample-sort1.csv', 'w', newline='') as fout5:

        read7 = DictReader(fin7)

        writenum6 = DictWriter(fout5, fieldnames=read7.fieldnames, extrasaction='ignore') 
        writenum6.writeheader()

        global alphlst

        for lime in alphlst:
           writenum6.writerow(lime)

        print("Write Complete")

我知道它很乱,而且我知道我现在正在做一些不必要的事情,那是因为我正在尝试使其工作并且还没有清理它。例如,所有打印仅用于故障排除。

这是输入文件的示例(虽然实际的更大):

Zone Name,IPStart,IPStop,CIDR,Source
Group A - Server,10.55.22.1/32,10.55.22.1/32,10.55.22.1/32,New List
Group A Sales Primary routers,10.23.1.0,10.23.1.15,10.24.1.0/28,New List
Group A Sales Web Servers - Primary,10.18.0.0,10.18.0.255,10.18.0.0/24,New List
Group A Marketing,10.62.0.0,10.62.255.255,10.62.0.0/16,New List
Group A Research,10.62.0.0,10.63.255.255,10.62.0.0/15,Old List
Group A Sales Web Servers,10.16.0.0,10.19.255.255,10.16.0.0/14,New List
Group A Sales,10.16.0.0,10.31.255.255,10.16.0.0/12,New List
Group A,10.0.0.0,10.127.255.255,10.0.0.0/9,New List
Group B,10.128.0.0,10.255.255.255,10.128.0.0/9,Old List

我已经发现问题出在代码的这一部分:

for line3 in SubnetDic1:
        line3['CIDR']=ipaddress.ip_network(line3['CIDR']) # Convert IP ranges to ip network object
        lst3.append(str(line3['CIDR']))
        for line4 in SubnetDic2:
            line4['CIDR']=ipaddress.ip_network(line4['CIDR']) # Convert IP ranges to ip network object
            Overlap(line3, line4) # Tests for IP overlap
            for val in lst:
                if str(val) not in lst3:
                    if str(val) not in lst4:
                        subwr=line3
                        subwr['CIDR']=val # New range
                        subwr['IPStart']=val.network_address #New net address
                        subwr['IPStop']=val.broadcast_address #New broadcast address
                        SubOut1.append(subwr)
                        print("{} is line3".format(subwr))
                        print("{} is line3 tst".format(line3))
                        lst4.append(str(val))



    print(SubOut1)
    print("^ SubOut1")

基本上,附加到SubOut1的值与应该的值不匹配。然而,它打印出print("{} is line3".format(subwr))print("{} is line3 tst".format(line3))两行中的内容。请记住,之前我使用“line3”作为要附加到列表而不是subwr的值,但这会产生相同的结果。

这是为print("{} is line3".format(subwr))行和print("{} is line3 tst".format(line3))行打印的示例:

{'Zone Name': 'Group A', 'IPStart': IPv4Address('10.64.0.0'), 'CIDR': IPv4Network('10.64.0.0/10'), 'Source': 'New List', 'IPStop': IPv4Address('10.127.255.255')} is line3
{'Zone Name': 'Group A', 'IPStart': IPv4Address('10.64.0.0'), 'CIDR': IPv4Network('10.64.0.0/10'), 'Source': 'New List', 'IPStop': IPv4Address('10.127.255.255')} is line3 tst

这就是“SubOut1”列表中的第一遍(当它应该在其中有上面一行时):

[{'Zone Name': 'Group A Research', 'IPStart': IPv4Address('10.63.0.0'), 'CIDR': IPv4Network('10.63.0.0/16'), 'Source': 'Old List', 'IPStop': IPv4Address('10.63.255.255')}, {'Zone Name': 'Group A Sales Web Servers', 'IPStart': IPv4Address('10.18.1.0'), 'CIDR': IPv4Network('10.18.1.0/24'), 'Source': 'New List', 'IPStop': IPv4Address('10.18.1.255')}, {'Zone Name': 'Group A Sales Web Servers', 'IPStart': IPv4Address('10.18.1.0'), 'CIDR': IPv4Network('10.18.1.0/24'), 'Source': 'New List', 'IPStop': IPv4Address('10.18.1.255')}, {'Zone Name': 'Group A Sales Web Servers', 'IPStart': IPv4Address('10.18.1.0'), 'CIDR': IPv4Network('10.18.1.0/24'), 'Source': 'New List', 'IPStop': IPv4Address('10.18.1.255')}, {'Zone Name': 'Group A Sales Web Servers', 'IPStart': IPv4Address('10.18.1.0'), 'CIDR': IPv4Network('10.18.1.0/24'), 'Source': 'New List', 'IPStop': IPv4Address('10.18.1.255')}, {'Zone Name': 'Group A Sales Web Servers', 'IPStart': IPv4Address('10.18.1.0'), 'CIDR': IPv4Network('10.18.1.0/24'), 'Source': 'New List', 'IPStop': IPv4Address('10.18.1.255')}, {'Zone Name': 'Group A Sales Web Servers', 'IPStart': IPv4Address('10.18.1.0'), 'CIDR': IPv4Network('10.18.1.0/24'), 'Source': 'New List', 'IPStop': IPv4Address('10.18.1.255')}, {'Zone Name': 'Group A Sales Web Servers', 'IPStart': IPv4Address('10.18.1.0'), 'CIDR': IPv4Network('10.18.1.0/24'), 'Source': 'New List', 'IPStop': IPv4Address('10.18.1.255')}, {'Zone Name': 'Group A Sales Web Servers', 'IPStart': IPv4Address('10.18.1.0'), 'CIDR': IPv4Network('10.18.1.0/24'), 'Source': 'New List', 'IPStop': IPv4Address('10.18.1.255')}, {'Zone Name': 'Group A Sales Web Servers', 'IPStart': IPv4Address('10.18.1.0'), 'CIDR': IPv4Network('10.18.1.0/24'), 'Source': 'New List', 'IPStop': IPv4Address('10.18.1.255')}, {'Zone Name': 'Group A Sales Web Servers', 'IPStart': IPv4Address('10.18.1.0'), 'CIDR': IPv4Network('10.18.1.0/24'), 'Source': 'New List', 'IPStop': IPv4Address('10.18.1.255')}, {'Zone Name': 'Group A Sales', 'IPStart': IPv4Address('10.24.1.16'), 'CIDR': IPv4Network('10.24.1.16/28'), 'Source': 'New List', 'IPStop': IPv4Address('10.24.1.31')}, {'Zone Name': 'Group A Sales', 'IPStart': IPv4Address('10.24.1.16'), 'CIDR': IPv4Network('10.24.1.16/28'), 'Source': 'New List', 'IPStop': IPv4Address('10.24.1.31')}, {'Zone Name': 'Group A Sales', 'IPStart': IPv4Address('10.24.1.16'), 'CIDR': IPv4Network('10.24.1.16/28'), 'Source': 'New List', 'IPStop': IPv4Address('10.24.1.31')}, {'Zone Name': 'Group A Sales', 'IPStart': IPv4Address('10.24.1.16'), 'CIDR': IPv4Network('10.24.1.16/28'), 'Source': 'New List', 'IPStop': IPv4Address('10.24.1.31')}, {'Zone Name': 'Group A Sales', 'IPStart': IPv4Address('10.24.1.16'), 'CIDR': IPv4Network('10.24.1.16/28'), 'Source': 'New List', 'IPStop': IPv4Address('10.24.1.31')}, {'Zone Name': 'Group A Sales', 'IPStart': IPv4Address('10.24.1.16'), 'CIDR': IPv4Network('10.24.1.16/28'), 'Source': 'New List', 'IPStop': IPv4Address('10.24.1.31')}, {'Zone Name': 'Group A Sales', 'IPStart': IPv4Address('10.24.1.16'), 'CIDR': IPv4Network('10.24.1.16/28'), 'Source': 'New List', 'IPStop': IPv4Address('10.24.1.31')}, {'Zone Name': 'Group A Sales', 'IPStart': IPv4Address('10.24.1.16'), 'CIDR': IPv4Network('10.24.1.16/28'), 'Source': 'New List', 'IPStop': IPv4Address('10.24.1.31')}, {'Zone Name': 'Group A Sales', 'IPStart': IPv4Address('10.24.1.16'), 'CIDR': IPv4Network('10.24.1.16/28'), 'Source': 'New List', 'IPStop': IPv4Address('10.24.1.31')}, {'Zone Name': 'Group A Sales', 'IPStart': IPv4Address('10.24.1.16'), 'CIDR': IPv4Network('10.24.1.16/28'), 'Source': 'New List', 'IPStop': IPv4Address('10.24.1.31')}, {'Zone Name': 'Group A Sales', 'IPStart': IPv4Address('10.24.1.16'), 'CIDR': IPv4Network('10.24.1.16/28'), 'Source': 'New List', 'IPStop': IPv4Address('10.24.1.31')}, {'Zone Name': 'Group A Sales', 'IPStart': IPv4Address('10.24.1.16'), 'CIDR': IPv4Network('10.24.1.16/28'), 'Source': 'New List', 'IPStop': IPv4Address('10.24.1.31')}, {'Zone Name': 'Group A Sales', 'IPStart': IPv4Address('10.24.1.16'), 'CIDR': IPv4Network('10.24.1.16/28'), 'Source': 'New List', 'IPStop': IPv4Address('10.24.1.31')}, {'Zone Name': 'Group A Sales', 'IPStart': IPv4Address('10.24.1.16'), 'CIDR': IPv4Network('10.24.1.16/28'), 'Source': 'New List', 'IPStop': IPv4Address('10.24.1.31')}, {'Zone Name': 'Group A Sales', 'IPStart': IPv4Address('10.24.1.16'), 'CIDR': IPv4Network('10.24.1.16/28'), 'Source': 'New List', 'IPStop': IPv4Address('10.24.1.31')}, {'Zone Name': 'Group A Sales', 'IPStart': IPv4Address('10.24.1.16'), 'CIDR': IPv4Network('10.24.1.16/28'), 'Source': 'New List', 'IPStop': IPv4Address('10.24.1.31')}, {'Zone Name': 'Group A', 'IPStart': IPv4Address('10.55.22.0'), 'CIDR': IPv4Network('10.55.22.0/32'), 'Source': 'New List', 'IPStop': IPv4Address('10.55.22.0')}, {'Zone Name': 'Group A', 'IPStart': IPv4Address('10.55.22.0'), 'CIDR': IPv4Network('10.55.22.0/32'), 'Source': 'New List', 'IPStop': IPv4Address('10.55.22.0')}, {'Zone Name': 'Group A', 'IPStart': IPv4Address('10.55.22.0'), 'CIDR': IPv4Network('10.55.22.0/32'), 'Source': 'New List', 'IPStop': IPv4Address('10.55.22.0')}, {'Zone Name': 'Group A', 'IPStart': IPv4Address('10.55.22.0'), 'CIDR': IPv4Network('10.55.22.0/32'), 'Source': 'New List', 'IPStop': IPv4Address('10.55.22.0')}, {'Zone Name': 'Group A', 'IPStart': IPv4Address('10.55.22.0'), 'CIDR': IPv4Network('10.55.22.0/32'), 'Source': 'New List', 'IPStop': IPv4Address('10.55.22.0')}, {'Zone Name': 'Group A', 'IPStart': IPv4Address('10.55.22.0'), 'CIDR': IPv4Network('10.55.22.0/32'), 'Source': 'New List', 'IPStop': IPv4Address('10.55.22.0')}, {'Zone Name': 'Group A', 'IPStart': IPv4Address('10.55.22.0'), 'CIDR': IPv4Network('10.55.22.0/32'), 'Source': 'New List', 'IPStop': IPv4Address('10.55.22.0')}, {'Zone Name': 'Group A', 'IPStart': IPv4Address('10.55.22.0'), 'CIDR': IPv4Network('10.55.22.0/32'), 'Source': 'New List', 'IPStop': IPv4Address('10.55.22.0')}, {'Zone Name': 'Group A', 'IPStart': IPv4Address('10.55.22.0'), 'CIDR': IPv4Network('10.55.22.0/32'), 'Source': 'New List', 'IPStop': IPv4Address('10.55.22.0')}, {'Zone Name': 'Group A', 'IPStart': IPv4Address('10.55.22.0'), 'CIDR': IPv4Network('10.55.22.0/32'), 'Source': 'New List', 'IPStop': IPv4Address('10.55.22.0')}, {'Zone Name': 'Group A', 'IPStart': IPv4Address('10.55.22.0'), 'CIDR': IPv4Network('10.55.22.0/32'), 'Source': 'New List', 'IPStop': IPv4Address('10.55.22.0')}, {'Zone Name': 'Group A', 'IPStart': IPv4Address('10.55.22.0'), 'CIDR': IPv4Network('10.55.22.0/32'), 'Source': 'New List', 'IPStop': IPv4Address('10.55.22.0')}, {'Zone Name': 'Group A', 'IPStart': IPv4Address('10.55.22.0'), 'CIDR': IPv4Network('10.55.22.0/32'), 'Source': 'New List', 'IPStop': IPv4Address('10.55.22.0')}, {'Zone Name': 'Group A', 'IPStart': IPv4Address('10.55.22.0'), 'CIDR': IPv4Network('10.55.22.0/32'), 'Source': 'New List', 'IPStop': IPv4Address('10.55.22.0')}, {'Zone Name': 'Group A', 'IPStart': IPv4Address('10.55.22.0'), 'CIDR': IPv4Network('10.55.22.0/32'), 'Source': 'New List', 'IPStop': IPv4Address('10.55.22.0')}, {'Zone Name': 'Group A', 'IPStart': IPv4Address('10.55.22.0'), 'CIDR': IPv4Network('10.55.22.0/32'), 'Source': 'New List', 'IPStop': IPv4Address('10.55.22.0')}, {'Zone Name': 'Group A', 'IPStart': IPv4Address('10.55.22.0'), 'CIDR': IPv4Network('10.55.22.0/32'), 'Source': 'New List', 'IPStop': IPv4Address('10.55.22.0')}, {'Zone Name': 'Group A', 'IPStart': IPv4Address('10.55.22.0'), 'CIDR': IPv4Network('10.55.22.0/32'), 'Source': 'New List', 'IPStop': IPv4Address('10.55.22.0')}, {'Zone Name': 'Group A', 'IPStart': IPv4Address('10.55.22.0'), 'CIDR': IPv4Network('10.55.22.0/32'), 'Source': 'New List', 'IPStop': IPv4Address('10.55.22.0')}, {'Zone Name': 'Group A', 'IPStart': IPv4Address('10.55.22.0'), 'CIDR': IPv4Network('10.55.22.0/32'), 'Source': 'New List', 'IPStop': IPv4Address('10.55.22.0')}, {'Zone Name': 'Group A', 'IPStart': IPv4Address('10.55.22.0'), 'CIDR': IPv4Network('10.55.22.0/32'), 'Source': 'New List', 'IPStop': IPv4Address('10.55.22.0')}, {'Zone Name': 'Group A', 'IPStart': IPv4Address('10.55.22.0'), 'CIDR': IPv4Network('10.55.22.0/32'), 'Source': 'New List', 'IPStop': IPv4Address('10.55.22.0')}, {'Zone Name': 'Group A', 'IPStart': IPv4Address('10.55.22.0'), 'CIDR': IPv4Network('10.55.22.0/32'), 'Source': 'New List', 'IPStop': IPv4Address('10.55.22.0')}]

之前使用的代码使用DictWriter写行而不是附加到列表。它是“write.writerow(line3)”而不是现在SubOut1.append(subwr)或同样的东西:SubOut1.append(line3)

有人可以帮我弄清楚为什么它没有将正确的值附加到SubOut1以及如何修复它?

由于

1 个答案:

答案 0 :(得分:2)

TL; DR

那就是说,这条线看起来很可疑:

subwr=line3

我认为应该是:

subwr=line3.copy()

问题是Python对象总是引用,所以如果你执行以下操作:

SubOut1 = []
line3 = {}

line3[0] = "usefuldata"
subwr = line3
SubOut1.append(subwr)

line3[0] = "uselessdata"

print SubOut1

你会得到{0:"useless data"}。因为所有subwrline3SubOut1[0]都是对同一个对象的引用。如果你修改这个对象,你将能够在任何地方看到这种变化。

将作业替换为subwr = line3.copy(),您将获得预期的结果。

部分偏离主题的说明:这就是为什么不应该这样做来初始化列表的原因:

a = b = c = []

但你必须这样做:

a = []
b = []
c = []