2013-05-25 33 views
3

我想创建一个字典从文件读取日期进一步处理,但无法让代码工作。我正在使用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 

现在,我要创建的字典对象为:

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

您的字典中的“L1”是什么?它被写成一个变量名,但事实并非如此。我想你想要一个字符串。 – Elazar

+2

你提供的是不是一个有效的'dict' –

+0

是L1是样本数据而不是变量。我想转换文件数据,基本上列到行,但太有选择性。 L1有两列,它们都被接受转换成(11,13)和(41,43)这样的行。但请注意,第4列数据在转换行中不重复。 – user2277675

回答

1

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

有什么方法可以找到Name1,L1的细节?基本上,L1是测试类型,说数学,其中[11,13]和[42,43]是分别尝试的术语标记,P27是复杂到简单的测试纸分级。现在,我想查找Name1-> L1测试详细信息“['L1',['11','13'],['41','43'],'P27']”。提前致谢! – user2277675

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)并在那里插入元素。

+1

'defaultdict(list)'在这里并不完全正确,因为OP需要类似'[L1,(11,13),(41,43),P27]' – jamylak

+0

我能够读取文件,但在创建字典转换时我失败了。 – user2277675

+0

@jamylak:是的,我想给一些提示,而不是完整的工作代码 –

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

在此之后,只要你喜欢,你可以冻结该结构为若干元组的形式。

+0

我不是很抱歉,但我不能遵循你的答案。请加几句解释。 – user2277675

+0

@UserSubir,新增说明希望更清楚 – perreal

+0

谢谢!我也会尝试解决这个问题。你们太棒了,非常有帮助! – user2277675