2 Python nested Dictionaries, current data in one the other will fill with the data from the previous minute displays. I need to diff int data

Question:

I’m really new to Python so I hope this makes sense.
These are a sample of 2 dictionaries. What I cannot work out is how to subtract the current entries from the previous entries where the nested dictionary ie "Name2" matches in the previous dictionary.
Also I cannot introduce or use extra libraries.

previous = {}

c = []
c.append("Date:07Nov22,Name:Name1,Type:InterTerm,C_Time:12.45.09,C_001:2873,C_002:2832,P_002:98.5,C_003:41,P_003:1.4,C_005:1,P_005:0.0,C_010:2873,C_011:8,P_011:0.2,C_012:9,P_012:0.3")
c.append("Date:07Nov22,Name:Name2,Type:InterTerm,C_Time:12.45.09,C_001:18981,C_002:18683,P_002:98.4,C_003:298,P_003:1.5,C_005:47,P_005:0.2,C_010:18981,C_011:39,P_011:0.2,C_012:86,P_012:0.4")
c.append("Date:07Nov22,Name:Name1,Type:InterTerm,C_Time:12.49.09,C_001:3145,C_002:3102,P_002:98.6,C_003:43,P_003:1.3,C_005:1,P_005:0.0,C_007:1,P_007:0.0,C_010:3145,C_011:12,P_011:0.3,C_012:13,P_012:0.4")
c.append("Date:07Nov22,Name:Name2,Type:InterTerm,C_Time:12.49.09,C_001:20742,C_002:20415,P_002:98.4,C_003:327,P_003:1.5,C_005:54,P_005:0.2,C_007:1,P_007:0.0,C_010:20742,C_011:42,P_011:0.2,C_012:96,P_012:0.4")
c.append("Date:07Nov22,Name:Name1,Type:InterTerm,C_Time:12.52.30,C_001:3357,C_002:3310,P_002:98.5,C_003:47,P_003:1.4,C_005:2,P_005:0.0,C_007:2,P_007:0.0,C_010:3357,C_011:13,P_011:0.3,C_012:15,P_012:0.4")
c.append("Date:07Nov22,Name:Name2,Type:InterTerm,C_Time:12.52.30,C_001:22176,C_002:21823,P_002:98.4,C_003:353,P_003:1.5,C_005:58,P_005:0.2,C_007:1,P_007:0.0,C_010:22176,C_011:44,P_011:0.1,C_012:102,P_012:0.4")

def setCMD():

     for cmd in c:
       print("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$")
       print("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$")
       print("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$")
       print("$$$                                                        $$$")
       print("$$$             THIS IS THIS THE START                     $$$")
       print("$$$                                                        $$$")
       print("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$")
       print("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$")
       print("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$")
       print("Using Command : ",cmd)

     
       zw = dict(item.split(':') for item in cmd.split(',')) 
       
       print("THIS IS ZW ",zw)
       
       modForInter(zw)


def calcDiff(d1,d2):
    print("In Function calDiff")
    for x in d2:
        for y in d1:
            if x==y:
                #print(d1[y],d2[x])
                for i in d1[y]:
                    for i in d2[x]:
                        d1[y][i]=d1[y][i]-d2[x][i]
                        #print(d1[y][i])
                    break
                return d1[y]



def modForInter(zw):
      print("In Mod modForInter")

      global previous
 
      nameDict = {}
      nameDict["Name"] = zw["Name"]
      
      print("This is nameDict",nameDict)
      zw1={ zw["Name"] } 
      print(zw1)                                                               
      
      # List for extraction.
      list1=["Date","Name","Type","C_Time",
          "C_001","","C_002","P_002","C_003","P_003","C_005","P_005",
          "C_007","P_007"]
      
      # List for prev
      list2=[
          "C_001","C_002","C_003","C_005","C_007"]
      
      current = {key:value for (key,value) in zw.items() if key in list1 }
       
      zw = {k: int(v) if isinstance(v, str) and v.isdigit() else v for k, v in zw.items()}

      current = {
      key1 : {key:value for (key,value) in zw.items() if key in list1 } for key1 in zw1 
      }                                                                                           
      print("current created")
      result = {
      key1 : {key:value for (key,value) in zw.items() if key in list1 } for key1 in zw1 
      }                                                                                           

      number_of_elements = sum(len(v) for v in current.values())
      if number_of_elements <= 4:
          print("Number of elements is less than 4")
          return
      else:
          print("Number of elements is greater than 4")
          
      y=(str(zw1).replace("{'","").replace("'}",""))
      print(y)  
               
      if "C_001" in previous[y]:
              print("previous is NOT empty")
              print("This is previous_dict :",previous)
              x=calcDiff(current,previous)
              print("This is X",x)
      else:
           print("previous_dict is empty and will be populated")
           previous.update({
      key1 : {key:value for (key,value) in zw.items() if key in list2 } for key1 in zw1 
      
      })
           print("THIS IS PREVIOUS_DICT : ",previous)
      
      
      del(current)
      del(zw)  
      print("THIS IS RESULT",result)
              


setCMD()

Notice that C_007 was added and was not in the previous dictionary and Name2 is gone also.

Asked By: C.Cab

||

Answers:

The calcDiff() function seems to be incorrect. I tried running your code but couldn’t get your expected output. Hence I modified your code as follows:

current =  {'Name2': {'Date': '07Nov2022', 'Name': 'Name2', 'Type': 'stats', 'Time': '12.49.09', 'C_001': 20742, 'C_002': 20415, 'P_002': '98.4', 'C_003': 327, 'P_003': '1.5', 'C_005': 54, 'P_005': '0.2', 'C_007': 1, 'P_007': '0.0'}}

previous = {'Name1': {'C_001': 3145, 'C_002': 3102, 'C_003': 43, 'C_005': 1, 'C_007': 1}, 'Name2': {'C_001': 18981, 'C_002': 18683, 'C_003': 298, 'C_005': 47}}

def calcDiff(d1,d2):
    print("In Function calDiff")
    for x in d2:
        for y in d1:
            if x==y:
                #print(d1[y],d2[x])
                for i in d1[y]:
                    for i in d2[x]:
                        d1[y][i]=d1[y][i]-d2[x][i]
                        #print(d1[y][i])
                    break
                return d1[y]
                

if "C_001" in previous['Name2']:
        print("previous is NOT empty")
        print("This is previous_dict :",previous)
        x=calcDiff(current,previous)
        print(x)

I was able to get the result without creating another directory.

{'Date': '07Nov2022', 'Name': 'Name2', 'Type': 'stats', 'Time': '12.49.09', 'C_001': 1761, 'C_002': 1732, 'P_002': '98.4', 'C_003': 29, 'P_003': '1.5', 'C_005': 7, 'P_005': '0.2', 'C_007': 1, 'P_007': '0.0'}

Output:

enter image description here

Answered By: AlekhyaV – Intel

With the previous answer I was able to complete it.

previous = {}

c = []
c.append("Date:07Nov22,Name:Name1,Type:InterTerm,C_Time:12.45.09,C_001:2873,C_002:2832,P_002:98.5,C_003:41,P_003:1.4,C_005:1,P_005:0.0,C_010:2873,C_011:8,P_011:0.2,C_012:9,P_012:0.3")
c.append("Date:07Nov22,Name:Name2,Type:InterTerm,C_Time:12.45.09,C_001:18981,C_002:18683,P_002:98.4,C_003:298,P_003:1.5,C_005:47,P_005:0.2,C_010:18981,C_011:39,P_011:0.2,C_012:86,P_012:0.4")
c.append("Date:07Nov22,Name:Name1,Type:InterTerm,C_Time:12.49.09,C_001:3145,C_002:3102,P_002:98.6,C_003:43,P_003:1.3,C_005:1,P_005:0.0,C_007:1,P_007:0.0,C_010:3145,C_011:12,P_011:0.3,C_012:13,P_012:0.4")
c.append("Date:07Nov22,Name:Name2,Type:InterTerm,C_Time:12.49.09,C_001:20742,C_002:20415,P_002:98.4,C_003:327,P_003:1.5,C_005:54,P_005:0.2,C_007:1,P_007:0.0,C_010:20742,C_011:42,P_011:0.2,C_012:96,P_012:0.4")
c.append("Date:07Nov22,Name:Name1,Type:InterTerm,C_Time:12.52.30,C_001:3357,C_002:3310,P_002:98.5,C_003:47,P_003:1.4,C_005:2,P_005:0.0,C_007:2,P_007:0.0,C_010:3357,C_011:13,P_011:0.3,C_012:15,P_012:0.4")
c.append("Date:07Nov22,Name:Name2,Type:InterTerm,C_Time:12.52.30,C_001:22176,C_002:21823,P_002:98.4,C_003:353,P_003:1.5,C_005:58,P_005:0.2,C_007:1,P_007:0.0,C_010:22176,C_011:44,P_011:0.1,C_012:102,P_012:0.4")

def setCMD():

     for cmd in c:
       print("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$")
       print("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$")
       print("$$$                                                        $$$")
       print("$$$             THIS IS THIS THE START                     $$$")
       print("$$$                                                        $$$")
       print("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$")
       print("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$")
       zw = dict(item.split(':') for item in cmd.split(',')) 
       modForInter(zw)

def calcDiff(d1,d2):
    print("In Function calDiff")
    for x in d2:
        for y in d1:
            if x==y:
                #print(d1[y],d2[x])
                for i in d1[y]:
                    for i in d2[x]:
                        d1[y][i]=d1[y][i]-d2[x][i]
                        #print(d1[y][i])
                    break
                return d1[y]

def modForInter(zw):
      print("In Mod modForInter")
      global previous
 
      nameDict = {}
      nameDict["Name"] = zw["Name"]
      
      zw1={ zw["Name"] }                                                             
      
      # List for extraction.
      list1=["Date","Name","Type","C_Time",
          "C_001","","C_002","P_002","C_003","P_003","C_005","P_005",
          "C_007","P_007"]
      
      # List for prev
      list2=[
          "C_001","C_002","C_003","C_005","C_007"]
           
      zw = {k: int(v) if isinstance(v, str) and v.isdigit() else v for k, v in zw.items()}
      current = {key1 : {key:value for (key,value) in zw.items() if key in list1 } for key1 in zw1 }                                                                                           
      result = {}
                                                                                         
      number_of_elements = sum(len(v) for v in current.values())
      if number_of_elements <= 4:
          print("Number of elements is less than 4")
          return
      else:
          print("Number of elements is greater than 4")
          
      y=(str(zw1).replace("{'","").replace("'}",""))
      print(y)  
               
      try:
          YES = previous[y]['C_001']
          result=calcDiff(current,previous)
      except KeyError:
          print('The specified key does NOT exist')
          previous.update({key1 : {key:value for (key,value) in zw.items() if key in list2 } for key1 in zw1 })
           
      del(current)
      del(zw)  
      print("THIS IS RESULT",result)
              
setCMD()
Answered By: C.Cab