473,809 Members | 2,809 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Iterate over data to build dictionary

dshimer
136 Recognized Expert New Member
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 3524
bartonc
6,596 Recognized Expert Expert
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 Recognized Expert New Member
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 Recognized Expert Expert
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 Recognized Expert Moderator Specialist
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
3853
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 time error 76. The paths are valid. I tested them and there is a file with the extension specified. Any help is appreciated Marcelo Rizzo
2
4447
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 sorted columns. to do this at the moment i am using perl, which is very easy to do, and i want to see if python is as easy. so, the data i am using is some epiphyte population abundance data for particular sites, and it looks like this:
4
3648
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 optional files that I could open and parse. At the end, I would easily close off the files by iterating through the dictionary. ---< CODE FOLLOWS >--- optionalfiles = {fileAreaCode: "areacode.11", fileBuild: "build.11"}
6
2050
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
1938
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
29391
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 iterate through them?
10
2317
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 it serves up. I'd like some suggestions on good ways to do this, because in the real world it can be quite difficult. For example, if I'm rummaging around in a MySQL database, the table structure and the code that generates the SQL requests...
13
3421
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. The first column is always an English
14
2116
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 checkbox, since
0
9602
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10639
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10376
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10383
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10120
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9200
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5550
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3861
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3015
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.