在使用语句Tokeniz之后从列表列表中选择子列表

2024-04-25 14:26:24 发布

您现在位置:Python中文网/ 问答频道 /正文

所以我在列表中有一些句子,比如:

some_list = ['Joe is travelling via train.' 
             'Joe waited for the train, but the train was late.'
             'Even after an hour, there was no sign of the 
              train. Joe then went to talk to station master about the 
              train's situation.']

然后我使用了nltk的句子标记器,因为我想单独分析完整句子中的每个句子。所以现在O/P在列表格式中看起来像这样:

sent_tokenize_list = [['Joe is travelling via train.'],
                      ['Joe waited for the train,',
                       'but the train was late.'],
                      ['Even after an hour,',
                       'there was no sign of the 
                        train.',
                       'Joe then went to talk to station master about 
                        the train's situation.']]    

现在,从这个列表列表中,我如何选择包含1个以上句子的列表,即我的示例中的第2个和第3个列表,并以单独列表的形式使用它们。

即O/p应为

['Joe waited for the train,','but the train was late.'] 
['Even after an hour,','there was no sign of the train.',
 'Joe then went to talk to station master about the train's situation.']         

Tags: thetoan列表fortrain句子but
1条回答
网友
1楼 · 发布于 2024-04-25 14:26:24

您可以使用len检查列表中的句子数。你知道吗

例如:

sent_tokenize_list = [['Joe is travelling via train.'],
                      ['Joe waited for the train,',
                       'but the train was late.'],
                      ['Even after an hour,','there was no sign of the train.',"Joe then went to talk to station master about the train's situation."]]


print([i for i in sent_tokenize_list if len(i) >= 2]) 

输出:

[['Joe waited for the train,', 'but the train was late.'], ['Even after an hour,', 'there was no sign of the train.', "Joe then went to talk to station master about the train's situation."]]

相关问题 更多 >