469,647 Members | 1,769 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,647 developers. It's quick & easy.

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 3235
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

Post your reply

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

Similar topics

reply views Thread by Marcelo Rizzo | last post: by
2 posts views Thread by ben moretti | last post: by
6 posts views Thread by supercomputer | last post: by
1 post views Thread by Djam | last post: by
10 posts views Thread by Frank van Wensveen | last post: by
13 posts views Thread by liujiaping | last post: by
14 posts views Thread by tdahsu | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.