简体   繁体   English

将句子列表分成数组中的各个单词

[英]splitting a list of sentences into seperate words in a array

I have a list which consists of lines as 我有一个包含行的列表

lines =  ['The query complexity']

I need to select each word in a separate line every time I start loop 每次开始循环时,我都需要在单独的行中选择每个单词

lines =  ['The']
lines =  ['query']
lines =  ['complexity']

Please help me to obtain it as a list of seperate words. 请帮助我以单独的单词列表的形式获取它。

for i in array:
    print(array[0].split())

This would work: 这将工作:

lines =  ['The query complexity']
split = [word for word in lines[0].split(' ')]

for word in split:
    #your code

The variable lines that you show only has one line in it, but I assume from your question that it could be multiple lines. 您显示的变量lines中只有一行,但是从您的问题来看,我认为它可能是多行。 In that case, you need to loop over lines and then loop over the individual words within each line. 在这种情况下,您需要遍历lines ,然后遍历每行中的各个单词。

Copying your example, you could try the following: 复制您的示例,您可以尝试以下操作:

for line in lines:
    for word in line.split():
        print(word)

Is that what you are looking for? 那是您要找的东西吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM