Data format:
The data type is a list, and the index element of the list is a dictionary. A list organized by a dictionary.
Analysis:
1. Traverse the dictionary with the same key, and then get the corresponding value according to the same key.
2. Subtract values to form a new dictionary, redefine the dictionary, or update the existing dictionary.
##The format and example of CRC value comparison are as follows: compare ABCD dict ABCD list (old ABCD dict, new ABCD dict), the form of returned value is the same.
#Old_CRC_Dict = [{'10GE1/0/10':"60"},{'10GE1/0/11':"20"},{'10GE1/0/12':"80"}]
#New_CRC_Dict = [{'10GE1/0/10':"70"},{'10GE1/0/11':"40"},{'10GE1/0/12':"20"}]
The script is 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
Optimized 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