取得3个字词的词组

时间:2018-07-18 14:09:08

标签: python text words

我已经试图弄清楚这一点了。

我想采用较大的文本/字符串并将其拆分为3个单词的短语,然后将它们添加到数组中。

我尝试使用spilt(),但是它没有按我希望的那样工作。

我对doinig的想法是如何使其起作用:

从字符串中的前3个单词开始,当我得到它们时,我将其放在一个数组中并移动1个单词,然后获取接下来的3个单词,依此类推。

这样做不好吗?

亲切的问候:)

2 个答案:

答案 0 :(得分:2)

my_really_long_string = "this is a really long string"
split_string = my_really_long_string.split()
phrase_array = [" ".join(split_string[i:i+3]) for i in range(len(split_string) - 2)]

第一行仅代表您的字符串。

在那之后,假设在定义单词结尾时您所关心的就是所有空格。 (@andrew_reece对边缘案例的评论非常相关。)

下一个迭代在0到n-2的范围内,其中n是字符串的长度。它从split_string数组中提取了3个连续的单词,并用空格将它们连在一起。

这几乎肯定不是最快的处理方法,因为它具有拆分和联接的功能,但是非常简单。

>>> my_really_long_string = "this is a really long string"
>>> split_string = my_really_long_string.split()
>>> phrases = [" ".join(split_string[i:i+3]) for i in range(len(split_string) - 2)]
>>> 
>>> phrases
['this is a', 'is a really', 'a really long', 'really long string']
>>> 

答案 1 :(得分:1)

这可以工作。您可能要先去除字符的文本,而不要确定您的数据是什么。

x = 'alt bot cot dot eat fat got hot iot jot kot lot mot not'
x = [y for y in [x.strip().split(' ')[i:i+3] for i in range(0, len(x), 3)]]
相关问题