Python 用';n';输入

Python 用';n';输入,python,list,count,Python,List,Count,基本上,我想知道如何计算列表中的字母数,例如,让我们假设我在列表中添加单词“example”,然后让我们假设我想知道字母“e”被使用了多少次,给定e作为用户输入,我该如何写它,所以它说单词“example”中有两个e 到目前为止我 WordList = [] print("1. Enter A Word") print("2. Check Letter Or Vowel Times") userInput = input("Please Choose An Option: ") if use

基本上,我想知道如何计算列表中的字母数,例如,让我们假设我在列表中添加单词“example”,然后让我们假设我想知道字母“e”被使用了多少次,给定e作为用户输入,我该如何写它,所以它说单词“example”中有两个e

到目前为止我

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())
不使用内置函数
count()


我孤立了你的问题。为此使用“集合”计数器:

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)

如果要计算单个单词中的字母数,需要某种方法从
单词列表
中选择正确的单词。是否可以不使用诸如count之类的内置函数来完成此操作?有关如何操作的线索?我试图改变你的旧版本,但没有得到任何结果:/
l=list('example')

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

print(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)