3

我正在尝试创建一个 dict 从文件中读取日期以进行进一步处理,但无法使代码正常工作。我正在使用 python 和这种语言的新手。我的文件数据如下所示:

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

现在,我想将 dict 对象创建为:

{
    '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]
}
4

3 回答 3

1

要处理这些行,请使用

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

现在chunks将包含您的部分行。

您应该构建一个dict,甚至更好defaultdict(list),并在那里插入元素。

于 2013-05-25T21:16:31.610 回答
1
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]

输出

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']]

在此之后,您可以根据需要将此结构冻结为某种元组形式。

于 2013-05-25T21:33:51.673 回答
1

Adefaultdict对此类问题很有帮助,它允许您附加到字典条目,如果条目尚不存在,它将附加到一个空列表并将其放置在那里,而不是像往常一样抛出异常。以下是我使用它来处理您的数据的方式:

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']
]})
于 2013-05-25T21:38:09.723 回答