473,387 Members | 1,899 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 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 11130
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

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

Similar topics

5
by: Garry Hodgson | last post by:
a colleague of mine has seen an odd problem in some code of ours. we initially noticed it on webware, but in distilling a test case it seems to be strictly a python issue. in the real system, it...
37
by: ajikoe | last post by:
Hello, Is anyone has experiance in running python code to run multi thread parallel in multi processor. Is it possible ? Can python manage which cpu shoud do every thread? Sincerely Yours,...
7
by: Shane | last post by:
Hi, Thanks in advance for the help. I have been to many websites and tried several solutions to my problem, but have fixed part of it. It's time to come humbly to the newsgroups for help :-) ...
0
by: Tree menu using XML | last post by:
I have one XML file that has nodes and sub node and each and every node has the attribute call visible if its value is true then diplay this node else don't display thid node, but this condition i...
4
by: mimmo | last post by:
Hi! I should convert the accented letters of a string in the correspondent letters not accented. But when I compile with -Wall it give me: warning: multi-character character constant Do the...
3
by: Fuzzy.wonderdog | last post by:
Hello, I hope some one can help me, at least give me a little direction. I have the need to insert many records into the same table at once. Not very many data points (<5), but many (30+)...
5
by: Olly | last post by:
Hello Everyone! Could someone please have a look at my JS Form I posted below....Something wrong there, but I don't understand what's exactly. Many thanks. Olly ...
15
by: Jay | last post by:
I have a multi threaded VB.NET application (4 threads) that I use to send text messages to many, many employees via system.timer at a 5 second interval. Basically, I look in a SQL table (queue) to...
10
by: Wayne | last post by:
I wish to remove the last newline (if any) from a string. This is where I started: var text = "hello\n"; text = text.replace( /^(.*)\n?$/, "$1" ); This seems to work, but not if the test has...
0
by: Guilherme Polo | last post by:
On Wed, Sep 3, 2008 at 8:57 PM, Kevin McKinley <kem1723@yahoo.comwrote: Come on.. "help on lines 384-403", that is not a good way to look for help. You are supposed to post some minimal code that...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.