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

Creating a nested dictionary...

GTXY20
29
hello all,

I have the following text file:

1,a,good
1,a,bad
1,b,good
1,c,bad

would like to create a dictionary:

Expand|Select|Wrap|Line Numbers
  1. d={1:{'v1':(a,b,c), 'v2':(good)}}
have been trying:

Expand|Select|Wrap|Line Numbers
  1. for line in lines:
  2.     uh, tf, tp = line.split('%')
  3.     if uh in d:
  4.         f=d[uh]['v1']
  5.         if tf not in f:
  6.             f.append(tf)
  7.         p=d[uh]['v2']
  8.         if tp is not 'bad' and tp not in p:
  9.             p.append(tp)
  10.     else:
  11.         d[uh]={'v1':[], 'v2':[]}
unfortunately I keep getting where 'bad' is being appended to 'v2':

Expand|Select|Wrap|Line Numbers
  1. d={'1': {'v1': ['a', 'b', 'c'], 'v2': ['bad', 'good']}}
Oct 7 '07 #1
3 2639
GTXY20
29
actually the code reads as follows but I am still having the same problem:

Expand|Select|Wrap|Line Numbers
  1. infile = open('input.txt', 'r')
  2. records = infile.read()
  3. infile.close()
  4. lines = records.split()
  5. d={}
  6. for line in lines:
  7.     uh, tf, tp = line.split('%')
  8.     if uh in d:
  9.         f=d[uh]['v1']
  10.         if tf not in f:
  11.             f.append(tf)
  12.         p=d[uh]['v2']
  13.         if tp is not 'bad' and tp not in p:
  14.             p.append(tp)
  15.     else:
  16.         d[uh]={'v1':[tf], 'v2':[tp]}
Oct 7 '07 #2
bartonc
6,596 Expert 4TB
unfortunately I keep getting where 'bad' is being appended to 'v2':

Expand|Select|Wrap|Line Numbers
  1. d={'1': {'v1': ['a', 'b', 'c'], 'v2': ['bad', 'good']}}
I fail to see any logical reason that 'bad' would not be included in V2 since 'c' is in V1.
Oct 7 '07 #3
GTXY20
29
Oh I understand, thanks.

In order to remove these 'bad' entries I am thinkig of putting the follwing in after the dictionary is created to remove:

Expand|Select|Wrap|Line Numbers
  1. for key in d:
  2.     d[key]['v2'].remove('bad')
Oct 7 '07 #4

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

Similar topics

2
by: kbass | last post by:
I am new to Python and I am attempting to retrieve data from a database and I would like to place this data into a nested dictionary. After placing the data into a dictionary, I would like to loop...
6
by: B0nj | last post by:
I've got a class in which I want to implement a property that operates like an indexer, for the various colors associated with the class. For instance, I want to be able to do 'set' operations...
2
by: techiepundit | last post by:
I'm parsing some data of the form: OuterName1 InnerName1=5,InnerName2=7,InnerName3=34; OuterName2 InnerNameX=43,InnerNameY=67,InnerName3=21; OuterName3 .... and so on.... These are fake...
3
by: Jake Emerson | last post by:
I'm attempting to build a process that helps me to evaluate the performance of weather stations. The script below operates on an MS Access database, brings back some data, and then loops through to...
12
by: Rich Shepard | last post by:
I want to code what would be nested "for" loops in C, but I don't know the most elegant way of doing the same thing in python. So I need to learn how from you folks. Here's what I need to do: build...
1
by: Sam Loxton | last post by:
Hi, I am fairly new to the python language and am trying to sort a nested Dictionary of a Dictionary which I wish to sort by value. The dictionary does not have to be restructured as I only need...
1
by: hermesbaby | last post by:
I have a CSV-File which I convert to an array like this: , , , , , ]
1
by: qbob | last post by:
I have data that is essentially tree-like, and to store it/iterate over it I have been using nested dictionaries, eg: Dictionary<int, Dictionary<string, Dictionary<double>>> data = new ... to...
2
by: =?Utf-8?B?RGF2aWQgTW9ycmlz?= | last post by:
I am trying to create a nested Dictionary and get an error that seems odd to me. Here is my declaration: private IDictionary<Guid, IDictionary<Guid, string>> myNestedDictionary = new...
9
by: Andy B | last post by:
I need to decide on how to store online signed contracts for customers and clients. I have considered a database in sql server 2005, but it seems to be too complicated. Besides, if the contract...
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:
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: 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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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...
0
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...

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.