0
我是一名python初学者,并且遇到问题。我尝试从列表中获得前10.000项,但它返回了我错误的数据。我注意到,因为我第一次试图获得第一个10/20/50项目(我打印结果在.txt中),我什么都没有。这是一些代码。与假数据切片列表
/*for key in docs:
T_buffer=docs[key]['tf'].items()
keyset.append(T_buffer)
*/
keyseta = list(itertools.chain(*keyset))
keyseta.sort(key=lambda z: z[1], reverse=True)
keyset=keyseta[:8000]
/*
#t0=time.time() #timer ON
#for infile in docs: #upologismos idf kai tf_idf
# for word in docs[infile]['tf']:
# docs[infile]['idf'][word]= idf(word,voc)
# docs[infile]['tf_idf'][word]= docs[infile]['idf'][word] * docs[infile]['tf'][word]
#print time.time()-t0, "seconds"
*/
loglista1 = open("log1.txt",'w')
print >>loglista1, keyset
列表中包含的数据,如:
[('uwm', 0.034482758620689655),
('s', 0.034482758620689655),
('the', 0.034482758620689655),
('have', 0.034482758620689655)...]
'loglista1.close()'在哪里? –
dangg .... thnx !!!我忘了关闭文件。 –
最好使用['with'](http://docs.python.org/2/tutorial/inputoutput.html)语句和['file.write'](http://docs.python.org/ 2/library/stdtypes.html#file.write)将内容写入文件。 'print >>'在py3.x中不起作用。 –