473,231 Members | 2,031 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,231 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 11121
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: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...

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.