Comparative dictionary: a dictionary list elements are compared, as the key, subtracted

Data format:
data type list, a list of elements of the index dictionary. A list of dictionary organization.

Analysis:
1, with the dictionary traversal key, then the corresponding values obtained according to the same key.
2, subtracted form a new dictionary, the dictionary redefined, with new or existing dictionaries.

## CRC value comparison, format, and examples are as follows: Similarly Compare_Dict_List (Old_CRC_Dict, New_CRC_Dict), form the return value.
= #Old_CRC_Dict [{ '10GE1 / 0/10': "60"}, { '10GE1 / 0 /. 11': "20 is"}, { '10GE1 / 0/12 is': "80"}]
#New_CRC_Dict = [ { '10GE1 / 0/10' : "70"}, { '10GE1 / 0/11': "40"}, { '10GE1 / 0/12': "20"}]

Script reads as follows:

def Compare_Dict(Old_CRC_DictList,New_CRC_DictList):
    Temp_List=[]
    for x in range(len(Old_CRC_DictList)):
        #print (Old_CRC_DictList[x])
        for y in range(len(New_CRC_DictList)):
            #print (New_CRC_DictList[y])
            Temp_Dict = {}
            for k1,v1 in New_CRC_DictList[y].items():
                #print (k1)
                #print (Old_CRC_DictList[x][k1])
                if Old_CRC_DictList[x].get(k1):
                    Temp_Dict[k1] = int(v1)-int(Old_CRC_DictList[x][k1])
                    #if int(Old_CRC_DictList[x][k1])-int(v1) < 10:
                    #    print ("True")
                    print (Temp_Dict)
                    Temp_List.append(Temp_Dict)
    print (Temp_List)
    return Temp_List

After optimization script:

def Compare_DictList(Old_CRC_DictList,New_CRC_DictList):
    Temp_List=[]
    for x,y in zip(Old_CRC_DictList,New_CRC_DictList):
        Temp_Dict = {}
        for k1,v1 in y.items():
            if x.get(k1):
                Temp_Dict[k1] = int(v1)-int(x[k1])
                #if int(x[k1])-int(v1) < 10:
                #    print ("True")
                #print (Temp_Dict)
                Temp_List.append(Temp_Dict)
    print (Temp_List)
    return Temp_List

Guess you like

Origin blog.51cto.com/chier11/2415962