2017-11-15 8 views
-2

基本的にはリストの文字の数を数える方法を知りたがっています。たとえば、「EXAMPLE」という単語をリストに追加してからi何回「e」という文字が使用されているのかを知りたいのですが、ユーザー入力としてどのように書くのでしょうか?「EXAMPLE」という言葉に2つのeがあると言います。'n'入力を使ってリスト内の文字の数を調べる

は、これまでのところ、私はあなたの問題を分離count()

l=list('example') 

def count(y): 
    cnt=0 
    for x in l: 
     if x == y: 
      cnt+=1 
    return cnt 

print(count('e')) 
+0

単語を1文字に数えたい場合は、「WordList」から正しい単語を選択する方法が必要です。 –

答えて

0
l = ['example'] 

def count(y): 
    for x in l: 
     return x.count(y) 

count('e') 

を得ました。

from collections import Counter 

wordInput = input("Please Enter A Word: ").lower() 
wordDict = Counter(wordInput) # converts to dictionary with counts 

letterInput = input("Please Enter A Letter: ").lower() 

print(wordDict.get(letterInput,0)) # return counts of letter (0 if not found) 
+0

は、countのような組み込み関数を使わずに実行できますか? – TheProKi

+0

どのように手掛かりがありますか?私は周りの古いバージョンのURを微調整しようとしているが、結果が得られない:/ – TheProKi

1

組み込み関数を使用しない

WordList = [] 
print("1. Enter A Word") 
print("2. Check Letter Or Vowel Times") 


userInput = input("Please Choose An Option: ") 
if userInput == "1": 
    wordInput = input("Please Enter A Word: ") 
    WordList.append(wordInput.lower()) 
関連する問題