使用python将句子列表拆分为单词列表
我有一个句子列表使用python将句子列表拆分为单词列表,python,string,split,Python,String,Split,我有一个句子列表 s: 'hello everyone', 'how are you',..., 'i am fine'. 我想把这个句子列表分成一个单词列表 ['hello', 'everyone', 'how', 'are', 'you', .., 'i', 'am', 'fine] 因此,我的预期结果是: [['hello', 'everyone'], ['how', 'are', 'you'], .., ['i', 'am', 'fine]] 我试着这样: def split_l
s: 'hello everyone', 'how are you',..., 'i am fine'.
我想把这个句子列表分成一个单词列表
['hello', 'everyone', 'how', 'are', 'you', .., 'i', 'am', 'fine]
因此,我的预期结果是:
[['hello', 'everyone'], ['how', 'are', 'you'], .., ['i', 'am', 'fine]]
我试着这样:
def split_list(sentence):
for s in sentence:
s=s.split()
return s
但我有一张单子,不是单子
['hello', 'everyone', 'how', 'are', 'you', .., 'i', 'am', 'fine]
在函数
split_list
中,语句
所指的内容并不十分清楚,但如果它是一个字符串列表,如['hello everyone','how you','i am fine']
,那么每次迭代都会覆盖相同的字符串s
,最终得到最后一次迭代的结果,即[“我”、“我”、“很好”]
因此,您需要确保在列表列表中收集所有结果并返回
您可以这样做列表理解,假设它是一个字符串列表,如上所述
s = ['hello everyone', 'how are you', 'i am fine']
def split_list(sentence):
# Split each sentence in the list, and append to result list
return [item.split() for item in sentence]
print(split_list(s))
或者一个正常的for循环
s = ['hello everyone', 'how are you', 'i am fine']
def split_list(sentence):
result = []
#Split each sentence in the list, and append to result list
for s in sentence:
result.append(s.split())
return result
print(split_list(s))
两种情况下的输出相同
[['hello', 'everyone'], ['how', 'are', 'you'], ['i', 'am', 'fine']]
必须通过在循环之前初始化空列表并在循环中追加每个结果,将每次迭代的结果保存在列表中:
def split_list(sentence):
L = []
for s in sentence:
L.append(s.split())
return L
否则,函数将只返回上一次迭代的结果。这只能通过列表理解来完成
from nltk import word_tokenize
s = ['hello everyone', 'how are you', 'i am fine']
token = [word_tokenize(x) for x in s]
print(token)
o/p
[['hello', 'everyone'], ['how', 'are', 'you'], ['i', 'am', 'fine']]
s = ['hello everyone', 'how are you', 'i am fine']
s2 = [c.split() for c in s]
print(s2) # [['hello', 'everyone'], ['how', 'are', 'you'], ['i', 'am', 'fine']]
你的句子列表是什么样子的?问题还不太清楚。你能告诉我们你是如何调用函数@cyrinepersone
list(map(str.split,s))
将句子列表传递给你的函数不会返回你发布的结果吗