我有这个字符串
tw =('BenSasse, well I did teach her the bend-and-snap https://twitter.com/bethanyshondark/status/903301101855928322 QT @bethanyshondark Is Reese channeling @BenSasse https://acculturated.com/reese-witherspoons-daughter-something-many-celebrity-children-lack-work-ethic/ , Twitter for Android')
我需要创建一个包含所有3个以上元音的单词的列表。请帮忙!
答案 0 :(得分:1)
您可以将re.findall
与以下正则表达式配合使用:
import re
re.findall(r'(?:[a-z-]*[aeiou]){3,}[a-z-]*', tw, flags=re.IGNORECASE)
这将返回:
['BenSasse', 'bend-and-snap', 'bethanyshondark', 'bethanyshondark', 'Reese', 'channeling', 'BenSasse', 'acculturated', 'reese-witherspoons-daughter-something-many-celebrity-children-lack-work-ethic', 'Android']
答案 1 :(得分:1)
我建议您首先创建所有元音的列表:
vowels = ['a','e','i','o','u']
好吧,一个字母列表(Char)实际上与一个字符串相同,所以我只需要执行以下操作:
vowels = "aeiou"
之后,我将尝试将您的字符串拆分为单词。让我们按照Joran Beasley的建议尝试tw.split()
。它返回:
['BenSasse,', 'well', 'I', 'did', 'teach', 'her', 'the', 'bend-and-snap', 'https://twitter.com/bethanyshondark/status/903301101855928322', 'QT', '@bethanyshondark', 'Is', 'Reese', 'channeling', '@BenSasse', 'https://acculturated.com/reese-witherspoons-daughter-something-many-celebrity-children-lack-work-ethic/', ',', 'Twitter', 'for', 'Android']
将其作为您的“话语”,您还好吗?请注意,每个链接都是一个“单词”。我假设这很好。
好,因此,如果我们使用for循环访问每个单词,则可以使用内部for循环访问每个字母。但是在开始之前,我们需要使用3个或更多的元音来跟踪所有被接受的单词,因此请创建一个新列表:final_list = list()
。现在:
for word in tw.split():
counter=0 # Let's keep track of how many vowels we have in a word
for letter in word:
if letter in vowels:
counter = counter+1
if counter >= 3:
final_list.append(word) # Add the word if 3 or more vowels.
如果您现在进行打印:print(final_list)
,您应该得到:
['BenSasse,', 'bend-and-snap', 'https://twitter.com/bethanyshondark/status/903301101855928322', '@bethanyshondark', 'Reese', 'channeling', '@BenSasse', 'https://acculturated.com/reese-witherspoons-daughter-something-many-celebrity-children-lack-work-ethic/']