首页 > 解决方案 > 查找元音:重新编译模式不将单词的元音保持在一起

问题描述

这是我的代码:

import re

line = ("hello  moose pole  cccttt.ggg   cat   cow    car horse  dddfff arizona  mississippi   cleveland")

pattern = re.compile("[aAeEiIoOuU*]+", re.IGNORECASE)
while line:
    for word in pattern.findall(line):
        print(word+"\t"+"1")
    line = sys.stdin.readline()

出来的输出是: 输出

想要达到:

eo   1    
ooe  1    
oe   1    
     1    
a    1    
o    1    
a    1    
oe   1    
     1    
aioa  1    
iiii  1    
eea   1

注意:假设为没有元音的单词记录一个空白。至少我想收集元音。然后我可以解决非元音词的问题。

标签: pythonpython-3.x

解决方案


您正在寻找:

首先创建一个拆分值,冒号

a = re.sub('[^aeiou:]', '', re.sub(' +', ':', line), flags = re.I).split(':')

 for word in a: print(word+"\t"+"1")

eo      1
ooe     1
oe      1
        1
a       1
o       1
a       1
oe      1
        1
aioa    1
iiii    1
eea     1

推荐阅读