-1
我需要将此代码的打印值存储到变量中,以便我可以将其转储到文本文件中。将for循环的打印值存储在变量中if else else
for i, j, v in sorted(zip(m.row, m.col, m.data)):
if doc_id == -1:
print(str(j) + ':' + "{:.4f}".format(v), end=' ')
else:
if doc_id != i:
print()
print(str(j) + ':' + "{:.4f}".format(v), end=' ')
doc_id = i
我使用列表理解并将其附加到一个变量,但没有帮助。
如果你不熟悉变量存储值,你应该看看起来像[Python官方教程]教程(https://docs.python.org/3.5/教程/ index.html中)。 – TigerhawkT3
此外,这个问题中的代码是从[你以前的问题]的答案(http://stackoverflow.com/questions/40742105/converting-a-text-corpus-to-a-text-document-with -vocabulary-ID和 - 各自-TF)。这个网站不是一个编码服务;请做一些你自己的工作。 – TigerhawkT3