如何创建复杂类型dict obj

2024-10-01 17:33:56 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图创建一个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对象创建为:

^{pr2}$

Tags: 文件数据对象代码语言l1dictname1
3条回答
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]

输出

^{pr2}$

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

defaultdict对于这类问题很有帮助,它允许您附加到字典条目,如果某个条目还不存在,它将追加到一个空列表并将其放在那里,而不是像往常一样抛出异常。下面是我如何使用它来处理您的数据:

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

要处理这些行,请使用

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将包含您行的部分内容。在

您应该构建一个^{},或者更好的是{a2},并在那里插入元素。在

相关问题 更多 >

    热门问题