我正在尝试创建一个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]
}
答案 0 :(得分: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 :(得分: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']]
在此之后,您可以根据需要将此结构冻结为某种元组形式。
答案 2 :(得分:1)
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']
]})