471,349 Members | 1,271 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,349 software developers and data experts.

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):

Expand|Select|Wrap|Line Numbers
  1. reg_MAC_ADDRESS = {}
  2.  
  3. #inside loop
  4. if reg_MAC_ADDRESS.has_key((mac,event)):
  5.     reg_MAC_ADDRESS[mac,event] += 1
  6. else:
  7.     reg_MAC_ADDRESS[mac,event] = 1
  8.  
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...
Jul 26 '07 #1
7 10838
ilikepython
844 Expert 512MB
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):

Expand|Select|Wrap|Line Numbers
  1. reg_MAC_ADDRESS = {}
  2.  
  3. #inside loop
  4. if reg_MAC_ADDRESS.has_key((mac,event)):
  5.     reg_MAC_ADDRESS[mac,event] += 1
  6. else:
  7.     reg_MAC_ADDRESS[mac,event] = 1
  8.  
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.
Expand|Select|Wrap|Line Numbers
  1. if reg_MAC_ADDRESS.has_key(mac):
  2.     reg_MAC_ADDRESS[mac][event] += 1
  3. else:
  4.     reg_MAC_ADDRESS[mac] = dict(event = 1)
  5.  
I'm pretty sure that will work.
Does it work for you?
Jul 26 '07 #2
bvdet
2,851 Expert Mod 2GB
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):

Expand|Select|Wrap|Line Numbers
  1. reg_MAC_ADDRESS = {}
  2.  
  3. #inside loop
  4. if reg_MAC_ADDRESS.has_key((mac,event)):
  5.     reg_MAC_ADDRESS[mac,event] += 1
  6. else:
  7.     reg_MAC_ADDRESS[mac,event] = 1
  8.  
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:
Expand|Select|Wrap|Line Numbers
  1. if dd.has_key(mac):
  2.     if dd[mac].has_key(event):
  3.         dd[mac][event] += 1
  4.     else:
  5.         dd[mac].update({event: 1})
  6. else:
  7.     dd[mac] = {event: 1}
Example output:
>>> dd
{'00:23:B6:58:74: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}}
>>>
Jul 26 '07 #3
bvdet
2,851 Expert Mod 2GB
To print the dictionary:
Expand|Select|Wrap|Line Numbers
  1. for mkey in reg_MAC_ADDRESS:
  2.     for skey in reg_MAC_ADDRESS[mkey]:
  3.         print 'MAC: %s Event: %s Event Count: %d' % (mkey, skey, reg_MAC_ADDRESS[mkey][skey])
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
Jul 26 '07 #4
ilikepython
844 Expert 512MB
Here I am using 'dd' to represent the dictionary:
Expand|Select|Wrap|Line Numbers
  1. if dd.has_key(mac):
  2.     if dd[mac].has_key(event):
  3.         dd[mac][event] += 1
  4.     else:
  5.         dd[mac].update({event: 1})
  6. else:
  7.     dd[mac] = {event: 1}
Example output:
>>> dd
{'00:23:B6:58:74: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?
Jul 26 '07 #5
Replying to my own post, but I figured it out.

Expand|Select|Wrap|Line Numbers
  1. reg_MAC_ADDRESS = {}
  2.  
  3. # inside loop
  4. # build up the dictionary/database with the registration events and counts from log file
  5. if reg_MAC_ADDRESS.has_key(mobile_mac_address):
  6.     if reg_MAC_ADDRESS[mobile_mac_address].has_key(mobile_cause):
  7.         reg_MAC_ADDRESS[mobile_mac_address][mobile_cause] += 1
  8.     else:
  9.         reg_MAC_ADDRESS[mobile_mac_address][mobile_cause] = 1
  10. else:
  11.     reg_MAC_ADDRESS[mobile_mac_address] = {mobile_cause : 1}
  12.  
This is probably not the most elegant way of doing things, but it seems to work...
Jul 26 '07 #6
ilikepython
844 Expert 512MB
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.
Jul 26 '07 #7
bvdet
2,851 Expert Mod 2GB
Replying to my own post, but I figured it out.

Expand|Select|Wrap|Line Numbers
  1. reg_MAC_ADDRESS = {}
  2.  
  3. # inside loop
  4. # build up the dictionary/database with the registration events and counts from log file
  5. if reg_MAC_ADDRESS.has_key(mobile_mac_address):
  6.     if reg_MAC_ADDRESS[mobile_mac_address].has_key(mobile_cause):
  7.         reg_MAC_ADDRESS[mobile_mac_address][mobile_cause] += 1
  8.     else:
  9.         reg_MAC_ADDRESS[mobile_mac_address][mobile_cause] = 1
  10. else:
  11.     reg_MAC_ADDRESS[mobile_mac_address] = {mobile_cause : 1}
  12.  
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! :)
Jul 26 '07 #8

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

37 posts views Thread by ajikoe | last post: by
7 posts views Thread by Shane | last post: by
reply views Thread by Tree menu using XML | last post: by
3 posts views Thread by Fuzzy.wonderdog | last post: by
5 posts views Thread by Olly | last post: by
15 posts views Thread by Jay | last post: by
10 posts views Thread by Wayne | last post: by
reply views Thread by Guilherme Polo | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.