计算字符串中字母出现的频率 (Python)
Counting the Frequency of Letters in a string (Python)
所以我试图在不使用 python 词典的情况下计算用户输入字符串中字母的频率...
我希望输出如下(以字母H为例)
"The letter H occurred 1 time(s)."
我遇到的问题是程序按字母顺序打印字母
但我希望程序按照输入中给定的顺序打印字母的频率...
例如,如果我输入 "Hello"
该程序将打印
"The letter e occurred 1 time(s)"
"The letter h occurred 1 time(s)"
"The letter l occurred 2 time(s)"
"The letter o occurred 1 time(s)"
但我希望输出如下
"The letter h occurred 1 time(s)"
"The letter e occurred 1 time(s)"
"The letter l occurred 2 time(s)"
"The letter o occurred 1 time(s)"
这是我目前拥有的代码:
originalinput = input("")
if originalinput == "":
print("There are no letters.")
else:
word = str.lower(originalinput)
Alphabet= ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z']
for i in range(0,26):
if word.count(Alphabet[i]) > 0:
print("The letter", Alphabet[i] ,"occured", word.count(Alphabet[i]), "times")
您可以使用 if else
检查输入,如果需要,可以使用自定义消息 raise
检查错误
if original_input == "":
raise RuntimeError("Empty input")
else:
# Your code goes there
作为方不,input()
就够了,不用加引号""
编辑:此问题已编辑,原问题是检查输入是否为空。
第二次编辑:
如果您希望您的代码在输出中打印字母,您应该遍历单词而不是字母表:
for c in word:
print("The letter", c ,"occured", word.count(c), "times")
正如@BlueSheepToken 提到的,您可以使用简单的 if
else
语句。下面是您的代码,其中包含提到的内容。
from collections import defaultdict
originalinput = input()
if originalinput == "":
raise RuntimeError("Empty input")
else:
result = defaultdict(int)
for char in list(originalinput.lower()):
result[char] += 1
for letter, num in result.items():
print(f'The letter {letter} occurred {num} time(s)')
#Hello
#The letter h occurred 1 time(s)
#The letter e occurred 1 time(s)
#The letter l occurred 2 time(s)
#The letter o occurred 1 time(s)
在这种情况下使用 defaultdict
会有所帮助,因为所有未使用的 key
都将是 0
.
我建议使用集合库中的计数器。
from collections import Counter
print(Counter('sample text'))
#Output: Counter({'t': 2, 'e': 2, 'l': 1, 's': 1, 'a': 1, ' ': 1, 'p': 1, 'm': 1, 'x': 1})
所以我试图在不使用 python 词典的情况下计算用户输入字符串中字母的频率... 我希望输出如下(以字母H为例)
"The letter H occurred 1 time(s)."
我遇到的问题是程序按字母顺序打印字母 但我希望程序按照输入中给定的顺序打印字母的频率...
例如,如果我输入 "Hello" 该程序将打印
"The letter e occurred 1 time(s)"
"The letter h occurred 1 time(s)"
"The letter l occurred 2 time(s)"
"The letter o occurred 1 time(s)"
但我希望输出如下
"The letter h occurred 1 time(s)"
"The letter e occurred 1 time(s)"
"The letter l occurred 2 time(s)"
"The letter o occurred 1 time(s)"
这是我目前拥有的代码:
originalinput = input("")
if originalinput == "":
print("There are no letters.")
else:
word = str.lower(originalinput)
Alphabet= ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z']
for i in range(0,26):
if word.count(Alphabet[i]) > 0:
print("The letter", Alphabet[i] ,"occured", word.count(Alphabet[i]), "times")
您可以使用 if else
检查输入,如果需要,可以使用自定义消息 raise
检查错误
if original_input == "":
raise RuntimeError("Empty input")
else:
# Your code goes there
作为方不,input()
就够了,不用加引号""
编辑:此问题已编辑,原问题是检查输入是否为空。
第二次编辑:
如果您希望您的代码在输出中打印字母,您应该遍历单词而不是字母表:
for c in word:
print("The letter", c ,"occured", word.count(c), "times")
正如@BlueSheepToken 提到的,您可以使用简单的 if
else
语句。下面是您的代码,其中包含提到的内容。
from collections import defaultdict
originalinput = input()
if originalinput == "":
raise RuntimeError("Empty input")
else:
result = defaultdict(int)
for char in list(originalinput.lower()):
result[char] += 1
for letter, num in result.items():
print(f'The letter {letter} occurred {num} time(s)')
#Hello
#The letter h occurred 1 time(s)
#The letter e occurred 1 time(s)
#The letter l occurred 2 time(s)
#The letter o occurred 1 time(s)
在这种情况下使用 defaultdict
会有所帮助,因为所有未使用的 key
都将是 0
.
我建议使用集合库中的计数器。
from collections import Counter
print(Counter('sample text'))
#Output: Counter({'t': 2, 'e': 2, 'l': 1, 's': 1, 'a': 1, ' ': 1, 'p': 1, 'm': 1, 'x': 1})