我正在运行以下代码:
from nltk.tokenize import word_tokenize
from nltk.stem.porter import PorterStemmer
word = 'gardening'
tokens = word_tokenize(word.lower())
stemmer = PorterStemmer() # write code here
stemmed = [stemmer.stem(token) for token in tokens] # write your code here
print(stemmed)
我得到了输出['garden'],但我怎么能得到没有括号和引号的输出,即garden
我得到了输出['garden'],但我怎么能得到没有括号和引号的输出,即garden
您可以使用普通列表索引来获取结果。例如:
from nltk.tokenize import word_tokenize
from nltk.stem.porter import PorterStemmer
word = 'gardening'
tokens = word_tokenize(word.lower())
stemmer = PorterStemmer() # write code here
stemmed = [stemmer.stem(token) for token in tokens] # write your code here
# Print the first item in the list
print(stemmed[0])
:
garden
或者,对于较长的列表,可以使用for循环输出每个值:
from nltk.tokenize import word_tokenize
from nltk.stem.porter import PorterStemmer
word = 'gardening'
tokens = word_tokenize(word.lower())
stemmer = PorterStemmer() # write code here
stemmed = [stemmer.stem(token) for token in tokens] # write your code here
for s in stemmed:
print(s)
from nltk.tokenize import word_tokenize
from nltk.stem.porter import PorterStemmer
word = 'gardening'
tokens = word_tokenize(word.lower())
stemmer = PorterStemmer() # write code here
stemmed = [stemmer.stem(token) for token in tokens] # write your code here
for s in stemmed:
print(s)