[英]how set a word limit in paragraph's sentences in python?
當 append 在一個列表中時需要設置一個限制。
sent = 'Python is dynamically-typed and garbage-collected. It supports multiple programming paradigms, including structured (particularly procedural), object-oriented and functional programming.'
我只需要在一個句子中設置 5 個單詞和 append 到一個列表
output 應該 -
sent_list = ['Python is dynamically-typed and garbage-collected.', 'It supports multiple programming paradigms,', 'including structured (particularly procedural), object-oriented', 'and functional programming.']
嘗試這個:
words = sent.split(' ')
sent_list = [' '.join(words[n:n+5]) for n in range(0, len(words), 5)]
可能有點不正統:
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>... )
: 修飾,創建一個命名的捕獲組。([^\s]+\s){5}
:查找非空白字符序列(一個或多個),后跟一個空格; 然后重復5次。|.+$
:一旦第一個選項用盡,只需將最后一位完成。 我們使用re.finditer
循環所有match objects
並使用x.group('pattern')
獲取匹配。 除了最后一場比賽之外,所有比賽最后都會有一個額外的空格; 擺脫它的一種方法是使用re.sub
。
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)
Output:
['Python is dynamically-typed and garbage-collected.', 'It supports multiple programming paradigms,', 'including structured (particularly procedural), object-oriented', 'and functional programming.']
True
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.