首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在python中,如何在段落的句子中设置单词限制?

在python中,如何在段落的句子中设置单词限制?
EN

Stack Overflow用户
提问于 2022-09-07 10:31:58
回答 3查看 44关注 0票数 -2

在列表中追加时需要设置限制。

代码语言:javascript
复制
sent = 'Python is dynamically-typed and garbage-collected. It supports multiple programming paradigms, including structured (particularly procedural), object-oriented and functional programming.'

我只需要在一个句子中设置5个单词并附加到一个列表中。

产出应-

代码语言:javascript
复制
sent_list = ['Python is dynamically-typed and garbage-collected.', 'It supports multiple programming paradigms,', 'including structured (particularly procedural), object-oriented', 'and functional programming.']
EN

回答 3

Stack Overflow用户

发布于 2022-09-07 10:46:11

试试这个:

代码语言:javascript
复制
words = sent.split(' ')
sent_list = [' '.join(words[n:n+5]) for n in range(0, len(words), 5)]
票数 1
EN

Stack Overflow用户

发布于 2022-09-07 11:32:35

也许有点不正统:

代码语言:javascript
复制
sent_list = [re.sub(r'\s$','',x.group('pattern')) for x in 
     re.finditer('(?P<pattern>([^\s]+\s){5}|.+$)',sent)]

['Python is dynamically-typed and garbage-collected.',
 'It supports multiple programming paradigms,',
 'including structured (particularly procedural), object-oriented',
 'and functional programming.']

解释'(?P<pattern>([^\s]+\s){5}|.+$)'

  • (?P<pattern> ... ):化妆品,创建一个命名捕获group.
  • ([^\s]+\s){5}:查找序列的非空白字符(一个或多个)后的空白;然后重复5 times.
  • |.+$:,一旦第一个选项已经用完,只需将最后一位一直读到末尾。

我们使用re.finditer循环遍历所有的match objects,并使用x.group('pattern')获取匹配。除了最后一次匹配之外,所有这些都将在最后有一个额外的空格;消除它的一种方法是使用re.sub

票数 1
EN

Stack Overflow用户

发布于 2022-09-07 11:10:49

代码语言:javascript
复制
sent = 'Python is dynamically-typed and garbage-collected. It supports multiple programming paradigms, including structured (particularly procedural), object-oriented and functional programming.'
sent_list = ['Python is dynamically-typed and garbage-collected.', 
            'It supports multiple programming paradigms,', 
            'including structured (particularly procedural), object-oriented', 
            'and functional programming.']

new_list = []
inner_string = ""
sentence_list = sent.split(" ")
for idx, item in enumerate(sentence_list):
    if (idx+1)==1 or (idx+1)%5 != 0:
        if (idx+1) == len(sentence_list):
            inner_string += item
            new_list.append(inner_string)
        else:
            inner_string += item + " "
    elif (idx+1)!=1 and (idx+1) % 5 == 0 :
        inner_string += item
        new_list.append(inner_string)
        inner_string = ""
        
print(new_list)
print(new_list == sent_list)

输出:

代码语言:javascript
复制
['Python is dynamically-typed and garbage-collected.', 'It supports multiple programming paradigms,', 'including structured (particularly procedural), object-oriented', 'and functional programming.']
True
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73633987

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档