在列表中追加时需要设置限制。
sent = 'Python is dynamically-typed and garbage-collected. It supports multiple programming paradigms, including structured (particularly procedural), object-oriented and functional programming.'我只需要在一个句子中设置5个单词并附加到一个列表中。
产出应-
sent_list = ['Python is dynamically-typed and garbage-collected.', 'It supports multiple programming paradigms,', 'including structured (particularly procedural), object-oriented', 'and functional programming.']发布于 2022-09-07 10:46:11
试试这个:
words = sent.split(' ')
sent_list = [' '.join(words[n:n+5]) for n in range(0, len(words), 5)]发布于 2022-09-07 11:32:35
也许有点不正统:
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。
发布于 2022-09-07 11:10:49
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)输出:
['Python is dynamically-typed and garbage-collected.', 'It supports multiple programming paradigms,', 'including structured (particularly procedural), object-oriented', 'and functional programming.']
Truehttps://stackoverflow.com/questions/73633987
复制相似问题