簡體   English   中英

使用文本中的所有句子創建一個列表

[英]Create a list with all sentences from a text

如何從文本中創建一個包含所有句子的列表,其中一個句子需要至少 3 個單詞。 句子由".?!"分隔 . 例如:

text = "Hi! How are you? I am fine."

列表將是: ["How are you", "I am fine"]

你可以試試

import re

txt = "Hi! How are you? I am fine."
print([i[0].strip() for i in re.findall(r"((\W\w+){3,}(?=(\.|\!|\?)))", txt)])

Output

['How are you', 'I am fine']

此正則表達式從給定字符串中提取所有三個或更多由分隔的單詞。 或者? 或者 ?

您可以將split function 與正則表達式一起使用。

import re

s_nums = 'Hi! How are you? I am fine.'

print([sen for sen in re.split('\.|!|\?', s_nums) if len(sen) > 2])

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM