![](/img/trans.png)
[英]Installed Python2.7 on centos 6 and now pip points to Python2.7 instead of Python version
[英]UPDATE: Enumerating instead of summing in Python2.7
我在Python中有一个两部分的问题,需要帮助。我有以下两个输入。 第一个提供映射信息:即:
amphibian anm|art|art|art|art
anaconda anm
第二个提供要映射的数据:即:
amphibian first heador 10
anaconda first heador 2
anaconda second hello 1
该程序的目的是用对应的类别替换第二个文件的第一列中的单个单词,如在第一个文件的第二列中找到的那样。
因此,预期结果如下,其中考虑了与第一列中的概念相关的第二列和第三列的频率,并列举了每种可能性:
anm second hello 1.0
anm first heador 10.0
anm first heador 2.0
art first heador 10.0
art first heador 10.0
art first heador 10.0
art first heador 10.0
但是,在我的代码中,我遇到了以下问题:如果1、2和3列相同,则不是枚举第二列和第三列的各自类别,而是求和第四列的值, 如:
anm second hello 1.0
anm first heador 12.0
art first heador 40.0
如果其他列相同,则对第4列中的值求和。
我认为问题出在那部分代码在这里:
with open(infile, "rb") as input:
for line in input:
uLine = unicode(line, "latin1")
lemmaTAR, slot, filler, freq = uLine.split()
if lemmaTAR in lemmas:
senses = mapping[lemmaTAR].split(u'|')
sense_number = len(senses)
for sense in senses:
**classFreqs[sense][slot][filler] += int(freq)** #/ sense_number
else:
pass
在这种情况下,我尝试使用+ =递增变量,但是,它没有给我想要的结果。 当我尝试将classFreqs [sense] [slot] [filler]定义为classFreqs [sense] [slot] [filler] = int(freq)时 ,它仅考虑找到的第一个值,给我以下结果:
anm second hello 1
anm first heador 2
art first heador 10
这也不正确,因为它没有考虑索引输入中类别的不平衡。
有没有人对如何解决我遇到的这个快速的基本(但非常令人沮丧)问题提出建议? 先感谢您
UPDATE
我已经实现了@Hugh Bothwell的以下代码建议
产生的结果:
[(u'anm', u'first', u'heador', 10)]
[(u'anm', u'first', u'heador', 10), (u'art', u'first', u'heador', 10)]
[(u'anm', u'first', u'heador', 10), (u'art', u'first', u'heador', 10), (u'art', u'first', u'heador', 10)]
[(u'anm', u'first', u'heador', 10), (u'art', u'first', u'heador', 10), (u'art', u'first', u'heador', 10), (u'art', u'first', u'heador', 10)]
[(u'anm', u'first', u'heador', 10), (u'art', u'first', u'heador', 10), (u'art', u'first', u'heador', 10), (u'art', u'first', u'heador', 10), (u'art', u'first', u'heador', 10)]
[(u'anm', u'first', u'heador', 10), (u'art', u'first', u'heador', 10), (u'art', u'first', u'heador', 10), (u'art', u'first', u'heador', 10), (u'art', u'first', u'heador', 10), (u'anm', u'first', u'heador', 2)]
[(u'anm', u'first', u'heador', 10), (u'art', u'first', u'heador', 10), (u'art', u'first', u'heador', 10), (u'art', u'first', u'heador', 10), (u'art', u'first', u'heador', 10), (u'anm', u'first', u'heador', 2), (u'anm', u'second', u'hello', 1)]
因此,A。我不确定自己在做什么错... B.当我尝试打印输出时(即):
with open('output', 'wb') as oOutFile:
for sense in results:
for slot in results[sense]:
for fill in results[sense][slot]:
outstring = '\t'.join([sense, slot, fill,\
str(results[sense][slot][fill])])
oOutFile.write(outstring.encode("utf8") + '\n')
它给了我以下回溯错误:
Traceback (most recent call last):
File "SOcode_example.py", line 18, in <module>
for slot in results[sense]:
TypeError: list indices must be integers, not tuple
我在这里做错了什么? 谢谢。
lemmas = {}
with open("lemmas.txt", "rb") as inf:
for line in inf:
lem, senses = unicode(line, "latin1").split()
lemmas[lem] = senses.split("|")
results = []
with open("input.txt", "rb") as inf:
for line in inf:
lemmaTAR, slot, filler, freq = unicode(line, "latin1").split()
freq = int(freq)
for sense in lemmas.get(lemmaTAR, []):
results.append((sense, slot, filler, freq))
产生结果=
[
(u'anm', u'first', u'heador', 10),
(u'art', u'first', u'heador', 10),
(u'art', u'first', u'heador', 10),
(u'art', u'first', u'heador', 10),
(u'art', u'first', u'heador', 10),
(u'anm', u'first', u'heador', 2),
(u'anm', u'second', u'hello', 1)
]
编辑:查看您的输出,您显然将附加到结果,然后在每个步骤中打印累积结果。 无论是在每一步追加然后当完成打印时, 或在每一步打印; 不要尝试同时做。
with open('output', 'wb') as outf:
lines = ("\t".join(res) for res in results)
outf.write("\n".join(lines).encode("utf-8"))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.