簡體   English   中英

刪除包含元音的單詞

[英]Remove words containing vowels

我正在尋找 output 刪除了元音的字符串。

  • 輸入:我的名字是123
  • Output:我的123

我試過下面的代碼:

def without_vowels(sentence):
    vowels = 'aeiou'
    word = sentence.split()
    for l in word:
       for k in l:
          if k in vowels:
              l = ''

without_vowels('my name 123')

誰能給我使用列表壓縮的結果?

這是一種方法

def without_vowels(sentence):
    words = sentence.split()
    vowels = ['a', 'e', 'i', 'o', 'u']
    cleaned_words = [w for w in words if not any(v in w for v in vowels)]
    cleaned_string = ' '.join(cleaned_words)
    print(cleaned_string)

輸出my 123

如果具有如下所示的上字符,您可以使用帶有'a|e|i|o|u'.lower()搜索字符的regex

>>> import re

>>> st = 'My nAmE Is 123 MUe'

>>> [s for s in st.split() if not re.search(r'a|e|i|o|u',s.lower())]
['My', '123']

>>> ' '.join(s for s in st.split() if not re.search(r'a|e|i|o|u',s.lower()))
'My 123'
def rem_vowel(string):

    vowels = ['a','e','i','o','u']

    result = [letter for letter in string if letter.lower() not in vowels]

    result = ''.join(result)

    print(result)
string = "My name is 123"
rem_vowel(string)

進口重新

def rem_vowel(string):

return (re.sub("[aeiouAEIOU]","",string))            

驅動程序

string = "我是 uma Bhargav "

打印 rem_vowel(string)

這是我的回答:

def without_vowels(sentence):
    print(' '.join([j for j in sentence.split() if not any(v in j for v in ['a', 'e', 'i', 'o', 'u'])]))

without_vowels('My name is 123')

結果是: M nm s 123

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM