将句子放入列表中 - python
我知道 nltk 可以分割句子并使用以下代码将其打印出来。 但是我如何将句子放入列表中而不是输出到屏幕上?
import nltk.data
from nltk.tokenize import sent_tokenize
import os, sys, re, glob
cwd = './extract_en' #os.getcwd()
for infile in glob.glob(os.path.join(cwd, 'fileX.txt')):
(PATH, FILENAME) = os.path.split(infile)
read = open(infile)
for line in read:
sent_tokenize(line)
sent_tokenize(line) 将其打印出来。我如何将其放入列表中?
I understand that nltk can split sentences and print it out using the following code.
but how do i put the sentences into a list instead of outputing onto the screen?
import nltk.data
from nltk.tokenize import sent_tokenize
import os, sys, re, glob
cwd = './extract_en' #os.getcwd()
for infile in glob.glob(os.path.join(cwd, 'fileX.txt')):
(PATH, FILENAME) = os.path.split(infile)
read = open(infile)
for line in read:
sent_tokenize(line)
the sent_tokenize(line) prints it out. how do i put it into a list?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是我用来测试代码的简化版本:
当像这样调用时,它会打印以下内容:
当做这样的事情时,列表理解更简洁,IMO 更容易阅读:
为了澄清,上面返回了一个列表句子列表,每行一个句子列表。如果您想要一个简单的句子列表,请按照 eyquem 建议的那样执行此操作:
Here's a simplified version that I used to test the code:
When called like so, it prints the following:
When doing something like this, a list comprehension is more concise and IMO more pleasant to read:
To clarify, the above returns a list of lists of sentences, one list of sentences for each line. If you want a flat list of sentences, do this instead, as eyquem suggests:
您不得使用关键字名称(read)来命名程序的对象。
。
如果你想追加到列表中,你必须有一个列表:
或者使用列表理解
或使用Python工具
,或者我不明白你想要什么
编辑:
好吧,考虑到 Jochen Ritzel 的评论,你想要
You must not use a keyword name (read) to name an object of your programm.
.
If you want to append in a list, you must have a list:
or with a list comprehension
or using the tools of Python
or I didn't understand what you want
EDIT:
Well, considering the Jochen Ritzel 's remark, you want