我正在尝试运行以下代码。但是我没有得到我对term_double_score()函数调用的输出。我真的很感激为什么会发生这种情况以及如何查看函数调用的输出?
def term_double_score(sent_file):
scores_double = {} # initialize an empty dictionary with double phrases
for line in sent_file:
term, score = line.split("\t") # The file is tab-delimited.
if " " in term:
scores_double[term] = int(score)
print "Len_double:",len(scores_double.items())
def term_single_score(sent_file):
scores_single = {} # initialize an empty dictionary with single phrases
for line in sent_file:
term, score = line.split("\t") # The file is tab-delimited.
scores_single[term] = int(score)
print "Len_single:",len(scores_single.items())
sent_file = open("some.txt")
term_single_score(sent_file)
term_double_score(sent_file)
答案 0 :(得分:0)
在@ jonrsharpe的解决方案中,代码为single_score
读取并处理文件一次。在使用double_score
进行第二次处理之前,请使用sent_file.seek(0)
重置文件指针,以便代码可以重新读取该文件。
以下解决方案读取整个文件并对其进行缓冲。 Single_score
基于文件数据的副本运行。然后再次在副本上运行double_score
。
如果文件很小,后一种解决方案是好的;系统有足够的内存将其保存在内存中。对于巨大的文件,解决方案#1是优越的。
def term_double_score(sent_file):
scores_double = {} # initialize an empty dictionary with double phrases
for line in sent_file:
term, score = line.split("\t") # The file is tab-delimited.
if " " in term:
scores_double[term] = int(score)
print "Len_double:",len(scores_double.items())
def term_single_score(sent_file):
scores_single = {} # initialize an empty dictionary with single phrases
for line in sent_file:
term, score = line.split("\t") # The file is tab-delimited.
scores_single[term] = int(score)
print "Len_single:",len(scores_single.items())
# read file and buffer it
sent_file = list( open('zoot.dat') )
term_single_score(sent_file)
term_double_score(sent_file)