Help with multi-dimensional dictionary

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sgreszcz
    New Member
    • Jul 2007
    • 2

    Help with multi-dimensional dictionary

    I'm pretty new to python, and I'm working on a script that is reading in a log file and recording different registration events for each MAC address and the number of times they occur.

    I am trying to structure the record to look like this as the MAC addresses are unique:

    {'mac1' : { 'event1' : count1 , 'event2' : count2, ... } }

    The number of unique MAC addresses is variable, as well as the types of events that can happen.

    The best I've been able to do so far is this:

    {('mac1', 'event1') : count1, ('mac1', event2') : count2, ('mac2', 'event1') : count 1, ...}

    This makes it more difficult to print the output as I'd like to do this

    mac1: event1=count1 event2=count2 event3=count3
    mac2: event 2=count2 event3=count3
    mac3: event1=count1 event3= count3
    ...

    The code I have written so far (inside a loop which parses each logfile line):

    Code:
    reg_MAC_ADDRESS = {}
    
    #inside loop
    if reg_MAC_ADDRESS.has_key((mac,event)):
        reg_MAC_ADDRESS[mac,event] += 1
    else:
        reg_MAC_ADDRESS[mac,event] = 1
    I guess the next step when I sort out the record is how to get the data out of the record and print it to the screen or an output file

    Thanks in advance...
  • ilikepython
    Recognized Expert Contributor
    • Feb 2007
    • 844

    #2
    Originally posted by sgreszcz
    I'm pretty new to python, and I'm working on a script that is reading in a log file and recording different registration events for each MAC address and the number of times they occur.

    I am trying to structure the record to look like this as the MAC addresses are unique:

    {'mac1' : { 'event1' : count1 , 'event2' : count2, ... } }

    The number of unique MAC addresses is variable, as well as the types of events that can happen.

    The best I've been able to do so far is this:

    {('mac1', 'event1') : count1, ('mac1', event2') : count2, ('mac2', 'event1') : count 1, ...}

    This makes it more difficult to print the output as I'd like to do this

    mac1: event1=count1 event2=count2 event3=count3
    mac2: event 2=count2 event3=count3
    mac3: event1=count1 event3= count3
    ...

    The code I have written so far (inside a loop which parses each logfile line):

    Code:
    reg_MAC_ADDRESS = {}
    
    #inside loop
    if reg_MAC_ADDRESS.has_key((mac,event)):
        reg_MAC_ADDRESS[mac,event] += 1
    else:
        reg_MAC_ADDRESS[mac,event] = 1
    I guess the next step when I sort out the record is how to get the data out of the record and print it to the screen or an output file

    Thanks in advance...
    It is possible to have a dictionary with dictionaries in it.
    [code=python]
    if reg_MAC_ADDRESS .has_key(mac):
    reg_MAC_ADDRESS[mac][event] += 1
    else:
    reg_MAC_ADDRESS[mac] = dict(event = 1)
    [/code]
    I'm pretty sure that will work.
    Does it work for you?

    Comment

    • bvdet
      Recognized Expert Specialist
      • Oct 2006
      • 2851

      #3
      Originally posted by sgreszcz
      I'm pretty new to python, and I'm working on a script that is reading in a log file and recording different registration events for each MAC address and the number of times they occur.

      I am trying to structure the record to look like this as the MAC addresses are unique:

      {'mac1' : { 'event1' : count1 , 'event2' : count2, ... } }

      The number of unique MAC addresses is variable, as well as the types of events that can happen.

      The best I've been able to do so far is this:

      {('mac1', 'event1') : count1, ('mac1', event2') : count2, ('mac2', 'event1') : count 1, ...}

      This makes it more difficult to print the output as I'd like to do this

      mac1: event1=count1 event2=count2 event3=count3
      mac2: event 2=count2 event3=count3
      mac3: event1=count1 event3= count3
      ...

      The code I have written so far (inside a loop which parses each logfile line):

      Code:
      reg_MAC_ADDRESS = {}
      
      #inside loop
      if reg_MAC_ADDRESS.has_key((mac,event)):
          reg_MAC_ADDRESS[mac,event] += 1
      else:
          reg_MAC_ADDRESS[mac,event] = 1
      I guess the next step when I sort out the record is how to get the data out of the record and print it to the screen or an output file

      Thanks in advance...
      Here I am using 'dd' to represent the dictionary:[code=Python]
      if dd.has_key(mac) :
      if dd[mac].has_key(event) :
      dd[mac][event] += 1
      else:
      dd[mac].update({event: 1})
      else:
      dd[mac] = {event: 1}[/code]
      Example output:
      >>> dd
      {'00:23:B6:58:7 4:7B': {'FFF': 4, 'GGG': 2, 'DDD': 6}, '00:16:C6:99:23 :3D': {'FFF': 6, 'GGG': 3, 'DDD': 9}, '00:22:A6:58:79 :7C': {'FFF': 2, 'GGG': 1, 'DDD': 3}}
      >>>

      Comment

      • bvdet
        Recognized Expert Specialist
        • Oct 2006
        • 2851

        #4
        To print the dictionary:[code=Python]for mkey in reg_MAC_ADDRESS :
        for skey in reg_MAC_ADDRESS[mkey]:
        print 'MAC: %s Event: %s Event Count: %d' % (mkey, skey, reg_MAC_ADDRESS[mkey][skey])[/code]

        Output:

        >>> MAC: 00:23:B6:58:74: 7B Event: FFF Event Count: 4
        MAC: 00:23:B6:58:74: 7B Event: GGG Event Count: 2
        MAC: 00:23:B6:58:74: 7B Event: DDD Event Count: 6
        MAC: 00:16:C6:99:23: 3D Event: FFF Event Count: 6
        MAC: 00:16:C6:99:23: 3D Event: GGG Event Count: 3
        MAC: 00:16:C6:99:23: 3D Event: DDD Event Count: 9
        MAC: 00:22:A6:58:79: 7C Event: FFF Event Count: 2
        MAC: 00:22:A6:58:79: 7C Event: GGG Event Count: 1
        MAC: 00:22:A6:58:79: 7C Event: DDD Event Count: 3

        Comment

        • ilikepython
          Recognized Expert Contributor
          • Feb 2007
          • 844

          #5
          Originally posted by bvdet
          Here I am using 'dd' to represent the dictionary:[code=Python]
          if dd.has_key(mac) :
          if dd[mac].has_key(event) :
          dd[mac][event] += 1
          else:
          dd[mac].update({event: 1})
          else:
          dd[mac] = {event: 1}[/code]
          Example output:
          >>> dd
          {'00:23:B6:58:7 4:7B': {'FFF': 4, 'GGG': 2, 'DDD': 6}, '00:16:C6:99:23 :3D': {'FFF': 6, 'GGG': 3, 'DDD': 9}, '00:22:A6:58:79 :7C': {'FFF': 2, 'GGG': 1, 'DDD': 3}}
          >>>
          That's exactly what my original code looked like but, I didn't think there's a need for the nested if because everytime we create a new mac we give it an event. So every mac will have an event, so there's no need to check if it does. Am I right?

          Comment

          • sgreszcz
            New Member
            • Jul 2007
            • 2

            #6
            Replying to my own post, but I figured it out.

            Code:
            reg_MAC_ADDRESS = {}
            
            # inside loop
            # build up the dictionary/database with the registration events and counts from log file
            if reg_MAC_ADDRESS.has_key(mobile_mac_address):
                if reg_MAC_ADDRESS[mobile_mac_address].has_key(mobile_cause):
                    reg_MAC_ADDRESS[mobile_mac_address][mobile_cause] += 1
                else:
                    reg_MAC_ADDRESS[mobile_mac_address][mobile_cause] = 1
            else:
                reg_MAC_ADDRESS[mobile_mac_address] = {mobile_cause : 1}
            This is probably not the most elegant way of doing things, but it seems to work...

            Comment

            • ilikepython
              Recognized Expert Contributor
              • Feb 2007
              • 844

              #7
              Originally posted by ilikepython
              That's exactly what my original code looked like but, I didn't think there's a need for the nested if because everytime we create a new mac we give it an event. So every mac will have an event, so there's no need to check if it does. Am I right?
              Oh wow, I'm wrong. That was stupid, just because a mac has an event doesn't mean that it will have the one we need. So, I'm sorry.

              Comment

              • bvdet
                Recognized Expert Specialist
                • Oct 2006
                • 2851

                #8
                Originally posted by sgreszcz
                Replying to my own post, but I figured it out.

                Code:
                reg_MAC_ADDRESS = {}
                
                # inside loop
                # build up the dictionary/database with the registration events and counts from log file
                if reg_MAC_ADDRESS.has_key(mobile_mac_address):
                    if reg_MAC_ADDRESS[mobile_mac_address].has_key(mobile_cause):
                        reg_MAC_ADDRESS[mobile_mac_address][mobile_cause] += 1
                    else:
                        reg_MAC_ADDRESS[mobile_mac_address][mobile_cause] = 1
                else:
                    reg_MAC_ADDRESS[mobile_mac_address] = {mobile_cause : 1}
                This is probably not the most elegant way of doing things, but it seems to work...
                It looks fine to me. It's almost identical to mine! :)

                Comment

                Working...