2017-01-24 6 views
-1
def anti_vowel(text): 
    empL = [] #just an empty list 
    index = 0 #just an index counter 
    empS = "" #just an empty String 
    for i in text: 
     empL.append(i) # in this loop ill be adding the str passed in by "text" char by char to the empty list 
    else: # since this its a for/else loop this is also going to run 
     for char in empL: # iterates to each element in the list 
      if char in "aeiouAEIOU": **# if the element thats being iterated at the moment is cotained in this string** 
       empL.remove(char) #remove it 
     else: #yes this is going to run because it's another for/else 
      sizeEmpL = len(empL) # just the size of empty list 
      while sizeEmpL != 0 : 
       empS = empS + empL[index] 
       print(empS) 
       index += 1 
       sizeEmpL -= 1 
      print(empL) 
      print(empS) 

を失敗します文字列 "aeiouAEIOU"と比較された各charをチェックし、比較されたcharが "aeiouAEIOU"のいずれかを含んでいる場合、それを削除するはずです。 次に、要素を空の文字列に追加し、vowelless文字列を出力します。私は自分のオリジナルの方法で抗母音PROGを作成しようとしていますが、何らかの理由で1つの母音はprogがとても基本的に私は、PROGを置くことになっているパラメータとして文字列を渡すことになってる

+0

を助け

def no_vowels(input_string): return (''.join([x for x in input_string if x not in ['a','e','i','o','u','A','E','I','O','U']])) 

希望:あなたはそれの機能を作ることができます

'fjpfjpff jgpjfpj' 

? – blacksite

+0

予想される出力と実際の出力の入力を記述します。 –

+2

''' .join(c.lower()が 'aeiou'に含まれていない場合はcのテキスト)' –

答えて

1

繰り返し処理中に要素をリストから削除することは期待通りに機能しません。リストの反復処理中はリストを変更しないでください。ここに問題のコードがあります:

for char in empL: 
    if char in "aeiouAEIOU": 
     empL.remove(char) 

リストを反復しながらリストを短縮すると、リスト内の他の文字をスキップする効果があります。

+0

これは無視されているようですね@MarkTolonen –

+0

@frannyリストを作成して文字を削除し、元のテキストを繰り返し、非母音を新しい文字列に追加します。次に、イテレータを変更していません。また、upvotingと答えを受け入れることによってSOに感謝を与える:) –

0
string = 'ofiiajpfeiajpfeiaef ijgapijfpij' 

''.join([x for x in string if x not in ['a','e','i','o','u','A','E','I','O','U']]) 

出力:だから、あなたは単なる文字列から母音を削除することになっている

関連する問題