user2277675
user2277675

Reputation: 81

How to create a complex type dict object

I am trying to create a dict reading the date from a file for further processing but unable to get the code to work. I am working in python and new to this language. My file data looks like this:

Name1   L1  11  P27 41
Name1   L1  13  P27 43
Name1   L2  85  O60 125
Name1   L2  07  O60 107
Name1   L2  68  O60 118
Name1   L2  17  O60 117
Name1   L2  92  O60 192
Name2   L1  04  O60 84
Name2   L1  19  Z91 139
Name2   L2  32  Z91 332

Now, I want to create the dict object as:

{
    'Name1':[L1,(11,13),(41,43),P27],[L2,(85,07,68,17,92),(125,107,118,117,192),O60],
    'Name2':[L1,(19),(139),Z91],[L2,(32),(332),Z91]
}

Upvotes: 3

Views: 194

Answers (3)

perreal
perreal

Reputation: 97918

h=dict()
with open("input") as ifile:
    for l in ifile:
        n,c1,c2,c3,c4=l.split()
        # now, n=Name1   c1=L1  c2=11  c3=P27 c4=41
        # create a dict for h['Name1'] if it doesn't exist
        if n not in h: h[n] = dict()
        # create a row for h['Name1']['L1'] if it doesn't exist
        if c1 not in h[n]: h[n][c1] = [ [], [], [] ]
        # now we have h['Name1]['L1] = [ [], [], [] ]
        # add items to each column if that item does not exist there
        if c2 not in h[n][c1][0]: h[n][c1][0].append(c2)
        if c3 not in h[n][c1][1]: h[n][c1][1].append(c3)
        if c4 not in h[n][c1][2]: h[n][c1][2].append(c4)

for hh in h:
    for hhh in h[hh]:
        print hh, hhh, h[hh][hhh]

Output

Name2 L2 [['32'], ['Z91'], ['332']]
Name2 L1 [['04', '19'], ['O60', 'Z91'], ['84', '139']]
Name1 L2 [['85', '07', '68', '17', '92'], ['O60'], ['125', '107', '118', '117', '192']]
Name1 L1 [['11', '13'], ['P27'], ['41', '43']]

After this you can freeze this structure into some tuple form as you like.

Upvotes: 1

qwwqwwq
qwwqwwq

Reputation: 7309

A defaultdict is helpful for this sort of problem, it allows you to append to a dictionary entry, if an entry doesn't exist yet, it will append to an empty list and place it there, instead of throwing an exception as usual. Here's how I used it to process your data:

from collections import defaultdict

d=defaultdict(list)
with open("input.txt") as data:
    for line in data:
        line = line.strip().split()
        namelist = d[line[0]]
        try:
            idx = [x[0] for x in namelist].index(line[1])
        except:
            idx = -1
        if len(namelist) and idx >= 0:
            namelist[idx][1].append(line[2])
            namelist[idx][2].append(line[4])
        else:
            namelist.append([line[1], [line[2]], [line[4]], line[3]])

print d
>>> defaultdict(<type 'list'>, 
{'Name2': [
    ['L1', ['04', '19'], ['84', '139'], 'O60'], 
    ['L2', ['32'], ['332'], 'Z91']
], 
'Name1': [
    ['L1', ['11', '13'], ['41', '43'], 'P27'], 
    ['L2', ['85', '07', '68', '17', '92'], ['125', '107', '118', '117', '192'], 'O60']
]})

Upvotes: 1

Jakub M.
Jakub M.

Reputation: 33817

To process the lines, use

with open(filename) as file_handle: # open your file
    for line in file_handle:        # iterate over lines
        chunks = line.split()       # extract parts of the lines
        ...

Now chunks will contain parts of your line.

You should build a dict, or even better defaultdict(list) and insert the elements there.

Upvotes: 1

Related Questions