我很难找到所有有两个或更多元音的单词。到目前为止,这是我拥有的,但当我运行它,它没有给我任何输出。我很感激你的帮助。
import re
def main():
in_f = open("jobs-061505.txt", "r")
read = in_f.read()
in_f.close()
for word in read:
re.findall(r"\b[aAeEiIoOuU]*", read)
in_f = open("twoVoweledWordList.txt", "w")
in_f.write(word)
in_f.close()
print (word)
main()如果这不是正确的格式,我很抱歉。
发布于 2014-11-01 00:23:53
for word in read: <--- iterating over chars in "read"!
re.findall(r"\b[aAeEiIoOuU]*", read) <-- using read again, discarding result您的迭代和模式使用不对齐。另外,你不用结果。
考虑逐行处理文件等。
twovowels=re.compile(r".*[aeiou].*[aeiou].*", re.I)
nonword=re.compile(r"\W+", re.U)
file = open("filename")
for line in file:
for word in nonword.split(line):
if twovowels.match(word): print word
file.close()发布于 2014-11-01 00:23:56
使用re.findall函数查找至少包含两个元音的所有单词,
>>> s = """foo bar hghghg ljklj jfjgf o jgjh aei
bar oum"""
>>> re.findall(r'\S*?[aAeEiIoOuU]\S*?[aAeEiIoOuU]\S*', s)
['foo', 'aei', 'oum']
>>> re.findall(r'\w*?[aAeEiIoOuU]\w*?[aAeEiIoOuU]\w*', s)
['foo', 'aei', 'oum']发布于 2014-11-01 00:32:15
a='hello how are you"
[ x for x in a.split(' ') if len(re.findall('[aeiouAEIOU]',x))>=2 ]代码中的修改
import re
def main():
in_f = open("jobs-061505.txt", "r")
read = in_f.read()
words = [ x for x in re.findall('\w+',read) if len(re.finall('[aeiouAEIOU]',x))>=2 ]
print words在上面的代码中,'read()将把整个文件作为字符串读取。re.findall('\w+',read)会给出单词列表。如果列表的长度大于或等于2。它将以列表的形式存储。现在你可以对输出做任何事情了。
https://stackoverflow.com/questions/26685125
复制相似问题