如何仅检索元音开头的单词

发布于 2025-02-09 08:52:43 字数 1619 浏览 2 评论 0原文

我有两个字符串:

text_one = '"A Ukrainian American woman who lives near Boston, Massachusetts, told Fox News Digital on Monday that she can no longer speak on the phone with her own mother, who lives in southern Ukraine, because of the Russian attacks on Ukraine and the fear these attacks have engendered.'
text_two = '\n\nMany people in southern Ukraine — as well as throughout the country — are right now living in fear for their lives as Russian soldiers overtake the area, the Boston-area woman said."'

例如,我只需要检索以元音开头的单词:

乌克兰裔美国人在自己的乌克兰...等等。

从我提出过的一个问题来看,我一直在遇到进口re ,但对此尚无强烈掌握。我的想法是在 a []字符之后找到辅音的任何实例,然后将所有字符删除,直到下一个[]使用[]。到目前为止,我已经:

#import re library
import re

#establish strings, formatted for visual clarity
text_one = '"A Ukrainian American woman who lives near Boston, Massachusetts, told Fox News Digital on Monday that she can no longer speak on the phone with her own mother, who lives in southern Ukraine, because of the Russian attacks on Ukraine and the fear these attacks have engendered.'
text_two = '\n\nMany people in southern Ukraine — as well as throughout the country — are right now living in fear for their lives as Russian soldiers overtake the area, the Boston-area woman said."'

#define function to replace words that begin with a consonant
def replace(cons):
  return re.sub(r ##some kind of method to grab characters after [ ], beginning with consonant, ending at character before next [ ]##), ##some kind of lambda argument to sub characters for [] (nothing)##...

I have two strings:

text_one = '"A Ukrainian American woman who lives near Boston, Massachusetts, told Fox News Digital on Monday that she can no longer speak on the phone with her own mother, who lives in southern Ukraine, because of the Russian attacks on Ukraine and the fear these attacks have engendered.'
text_two = '\n\nMany people in southern Ukraine — as well as throughout the country — are right now living in fear for their lives as Russian soldiers overtake the area, the Boston-area woman said."'

I need to retrieve only the words that start with a vowel, for example:

A Ukranian American on on own Ukraine... etc.

From a previous question I had asked, I've been messing around with import re, but do not have a strong grasp on it yet. My idea is to find any instance of a consonant after a [ ] character and sub all characters up until the next [ ] with []. So far I have:

#import re library
import re

#establish strings, formatted for visual clarity
text_one = '"A Ukrainian American woman who lives near Boston, Massachusetts, told Fox News Digital on Monday that she can no longer speak on the phone with her own mother, who lives in southern Ukraine, because of the Russian attacks on Ukraine and the fear these attacks have engendered.'
text_two = '\n\nMany people in southern Ukraine — as well as throughout the country — are right now living in fear for their lives as Russian soldiers overtake the area, the Boston-area woman said."'

#define function to replace words that begin with a consonant
def replace(cons):
  return re.sub(r ##some kind of method to grab characters after [ ], beginning with consonant, ending at character before next [ ]##), ##some kind of lambda argument to sub characters for [] (nothing)##...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

带上头具痛哭 2025-02-16 08:52:43
import re
text_one = '"A Ukrainian American woman who lives near Boston, Massachusetts, told Fox News Digital on Monday that she can no longer speak on the phone with her own mother, who lives in southern Ukraine, because of the Russian attacks on Ukraine and the fear these attacks have engendered.'
list(map(lambda x: x.strip(), re.findall("\W[aieou][^ ]*", text_one, flags=re.I | re.M)))
import re
text_one = '"A Ukrainian American woman who lives near Boston, Massachusetts, told Fox News Digital on Monday that she can no longer speak on the phone with her own mother, who lives in southern Ukraine, because of the Russian attacks on Ukraine and the fear these attacks have engendered.'
list(map(lambda x: x.strip(), re.findall("\W[aieou][^ ]*", text_one, flags=re.I | re.M)))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文