Deleting consonants from a string in Python

前端 未结 3 779
情深已故
情深已故 2020-12-01 20:01

Here is my code. I\'m not exactly sure if I need a counter for this to work. The answer should be \'iiii\'.

def eliminate_consonants(x):
                


        
3条回答
  •  抹茶落季
    2020-12-01 20:38

    You can try pythonic way like this,

    In [1]: s = 'mississippi'
    In [3]: [char for char in s if char in 'aeiou']
    Out[3]: ['i', 'i', 'i', 'i']
    

    Function;

    In [4]: def eliminate_consonants(x):
       ...:     return ''.join(char for char in x if char in 'aeiou')
       ...: 
    
    In [5]: print(eliminate_consonants('mississippi'))
    iiii
    

提交回复
热议问题