473,385 Members | 1,641 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,385 software developers and data experts.

Iterate over data to build dictionary

dshimer
136 Expert 100+
I have a file whose structure in strictly generic terms is similar to the following.
Expand|Select|Wrap|Line Numbers
  1. keyname first
  2. keyword1 1.1
  3. keyword2 1.2
  4. keyword3 1.3
  5. keyname second
  6. keyword1 2.1
  7. keyword2 2.2
  8. keyword3 2.3
  9. keyname third
  10. keyword1 3.1
  11. keyword2 3.2
  12. keyword3 3.3
keyname value is always going to contain the name of a data set identified by keywords and values. The keywords are always the same for each keyname and contain data which is unique to the keyname. In the simplest terms if I read it like
Expand|Select|Wrap|Line Numbers
  1. >>> f=open('/tmp/test.txt','r')
  2. >>> d=f.readlines()
  3. >>> for l in d:
  4. ...     print l.split()
  5. ... 
  6. ['keyname', 'first']
  7. ['keyword1', '1.1']
  8. ['keyword2', '1.2']
  9. ['keyword3', '1.3']
  10. ['keyname', 'second']
  11. ['keyword1', '2.1']
  12. ['keyword2', '2.2']
  13. ['keyword3', '2.3']
  14. ['keyname', 'third']
  15. ['keyword1', '3.1']
  16. ['keyword2', '3.2']
  17. ['keyword3', '3.3']
What I would like to do is build a dictionary in which each keyname has a value which is another dictionary made up of keywords and values. For example if I were to manually build it the dictionary would look like
Expand|Select|Wrap|Line Numbers
  1. dict={'first':{'keyword1':1.1,'keyword2':1.2,'keyword3':1.3},'second':{'keyword1':2.1,'keyword2':2.2,'keyword3':2.3},'third':{'keyword1':3.1,'keyword2':3.2,'keyword3':3.3}}
allowing for access to whole keys, or individual data values like
Expand|Select|Wrap|Line Numbers
  1. >>> dict['second']
  2. {'keyword3': 2.2999999999999998, 'keyword2': 2.2000000000000002, 'keyword1': 2.1000000000000001}
  3. >>> dict['second']['keyword2']
  4. 2.2000000000000002
This is ripe for iterating over the data and adding as I go, if it were a list I would append, but I don't use dictionaries very often and don't know how to add/append/insert data. What is the best way to do this?
Mar 28 '07 #1
4 3498
bartonc
6,596 Expert 4TB
I have a file whose structure in strictly generic terms is similar to the following.
Expand|Select|Wrap|Line Numbers
  1. keyname first
  2. keyword1 1.1
  3. keyword2 1.2
  4. keyword3 1.3
  5. keyname second
  6. keyword1 2.1
  7. keyword2 2.2
  8. keyword3 2.3
  9. keyname third
  10. keyword1 3.1
  11. keyword2 3.2
  12. keyword3 3.3
keyname value is always going to contain the name of a data set identified by keywords and values. The keywords are always the same for each keyname and contain data which is unique to the keyname. In the simplest terms if I read it like
Expand|Select|Wrap|Line Numbers
  1. >>> f=open('/tmp/test.txt','r')
  2. >>> d=f.readlines()
  3. >>> for l in d:
  4. ...     print l.split()
  5. ... 
  6. ['keyname', 'first']
  7. ['keyword1', '1.1']
  8. ['keyword2', '1.2']
  9. ['keyword3', '1.3']
  10. ['keyname', 'second']
  11. ['keyword1', '2.1']
  12. ['keyword2', '2.2']
  13. ['keyword3', '2.3']
  14. ['keyname', 'third']
  15. ['keyword1', '3.1']
  16. ['keyword2', '3.2']
  17. ['keyword3', '3.3']
What I would like to do is build a dictionary in which each keyname has a value which is another dictionary made up of keywords and values. For example if I were to manually build it the dictionary would look like
Expand|Select|Wrap|Line Numbers
  1. dict={'first':{'keyword1':1.1,'keyword2':1.2,'keyword3':1.3},'second':{'keyword1':2.1,'keyword2':2.2,'keyword3':2.3},'third':{'keyword1':3.1,'keyword2':3.2,'keyword3':3.3}}
allowing for access to whole keys, or individual data values like
Expand|Select|Wrap|Line Numbers
  1. >>> dict['second']
  2. {'keyword3': 2.2999999999999998, 'keyword2': 2.2000000000000002, 'keyword1': 2.1000000000000001}
  3. >>> dict['second']['keyword2']
  4. 2.2000000000000002
This is ripe for iterating over the data and adding as I go, if it were a list I would append, but I don't use dictionaries very often and don't know how to add/append/insert data. What is the best way to do this?
It's simple:
Expand|Select|Wrap|Line Numbers
  1. aDict[newKeyName] = newValue
Scary, huh?
Mar 28 '07 #2
dshimer
136 Expert 100+
Good grief, I knew it had to be easy. I would be sorry for taking your time instead of digging through a book, but I guess it will be a good reference in case anybody else missed it. Python data types are just too cool and I'm finding that I like dictionaries more than I thought I would when I first started studying them.

Thanks
Mar 28 '07 #3
bartonc
6,596 Expert 4TB
Good grief, I knew it had to be easy. I would be sorry for taking your time instead of digging through a book, but I guess it will be a good reference in case anybody else missed it. Python data types are just too cool and I'm finding that I like dictionaries more than I thought I would when I first started studying them.

Thanks
Any time, D. It's really no trouble (and you contribute so much that the simplest to the toughest questions are yours for the asking). And as you say, it could help someone else along the way.
Mar 28 '07 #4
bvdet
2,851 Expert Mod 2GB
Good grief, I knew it had to be easy. I would be sorry for taking your time instead of digging through a book, but I guess it will be a good reference in case anybody else missed it. Python data types are just too cool and I'm finding that I like dictionaries more than I thought I would when I first started studying them.

Thanks
I like dictionaries also. I hope this helps you:
Expand|Select|Wrap|Line Numbers
  1. data = open(fn).read()
  2. dataLst = [i.strip() for i in data.split('keyname') if i != '']
  3. dd = {}
  4. for item in dataLst:
  5.     itemLst = item.split('\n')
  6.     dd[itemLst[0]] = dict(zip([i.split()[0] for i in itemLst[1:]], [j.split()[1] for j in itemLst[1:]]))
  7.  
  8. for key in dd:
  9.     print '%s = %s' % (key, dd[key])
  10.  
  11. '''
  12. >>> second = {'keyword3': '2.3', 'keyword2': '2.2', 'keyword1': '2.1'}
  13. third = {'keyword3': '3.3', 'keyword2': '3.2', 'keyword1': '3.1'}
  14. first = {'keyword3': '1.3', 'keyword2': '1.2', 'keyword1': '1.1'}
  15. >>>
  16. '''
I also apreciate your contributions. :)
Mar 29 '07 #5

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

Similar topics

0
by: Marcelo Rizzo | last post by:
I am trying to get the name of a file with a specific extension (tmw) from several different directories. The problem I am having is that the program stops working on the second pass with an run...
2
by: ben moretti | last post by:
hi i'm learning python, and one area i'd use it for is data management in scientific computing. in the case i've tried i want to reformat a data file from a normalised list to a matrix with some...
4
by: Julian Yap | last post by:
Hi all, I'm trying to get some ideas on the best way to do this. In this particular coding snippet, I was thinking of creating a dictionary of file objects and file names. These would be...
6
by: supercomputer | last post by:
I am using this function to parse data I have stored in an array. This is what the array looks like: , , , , , , , , , , , , , , , , , , , , , , , ] This is the code to parse the array:
1
by: Djam | last post by:
Hi, I need your help to build a dictionary on mysal database to have an efficient keyword search engine. Thanks :-) Djam
5
by: jaso | last post by:
Hi, If have a structure of a database record like this: struct record { char id; char title; ... }; Is there some way to find out how many member variables there is in the struct and then...
10
by: Frank van Wensveen | last post by:
Friend, coders, fellow wage slaves, lend my your ears. I believe that in a perfect world the design of a website (or feature on a website) should be totally separated from its design and the data...
13
by: liujiaping | last post by:
Hi, all. I have a dictionary-like file which has the following format: first 4 column 7 is 9 a 23 word 134 .... Every line has two columns....
14
by: tdahsu | last post by:
I have twenty-five checkboxes I need to create (don't ask): self.checkbox1 = ... self.checkbox2 = ... .. .. .. self.checkbox25 = ... Right now, my code has 25 lines in it, one for each...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...

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.