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中不起作用。 –