计算字符串中的字母频率 (Python)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/40985203/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Counting Letter Frequency in a String (Python)
提问by Kelvin San
I am trying to count the occurrences of each letter of a word
我正在尝试计算单词的每个字母的出现次数
word = input("Enter a word")
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):
print(word.count(Alphabet[i]))
This currently outputs the number of times each letter occurs including the ones that don't.
这当前输出每个字母出现的次数,包括没有出现的次数。
How do I list the letters vertically with the frequency alongside it e.g:
我如何垂直列出字母及其旁边的频率,例如:
word="Hello"
字=“你好”
H 1
1
E 1
1
L 2
2
O 1
1
回答by LMc
from collections import Counter
counts=Counter(word) # Counter({'l': 2, 'H': 1, 'e': 1, 'o': 1})
for i in word:
print(i,counts[i])
Try using Counter
, which will create a dictionary that contains the frequencies of all items in a collection.
尝试使用Counter
,这将创建一个字典,其中包含集合中所有项目的频率。
Otherwise, you could do a condition on your current code to print
only if word.count(Alphabet[i])
is greater than 0, though that would slower.
否则,您可以对当前代码设置条件,print
仅当word.count(Alphabet[i])
大于 0 时,尽管这会变慢。
回答by Uzam Hashmi
def char_frequency(str1):
dict = {}
for n in str1:
keys = dict.keys()
if n in keys:
dict[n] += 1
else:
dict[n] = 1
return dict
print(char_frequency('google.com'))
回答by duhaime
As @Pythonista said, this is a job for collections.Counter
:
正如@Pythonista 所说,这是一份工作collections.Counter
:
from collections import Counter
print(Counter('cats on wheels'))
This prints:
这打印:
{'s': 2, ' ': 2, 'e': 2, 't': 1, 'n': 1, 'l': 1, 'a': 1, 'c': 1, 'w': 1, 'h': 1, 'o': 1}
回答by soheshdoshi
easy and simple solution without lib.
没有lib的简单简单的解决方案。
string=input()
f={}
for i in string:
f[i]=f.get(i,0)+1
print(f)
here is the link for get()https://docs.quantifiedcode.com/python-anti-patterns/correctness/not_using_get_to_return_a_default_value_from_a_dictionary.html
回答by Shahriar Rahman Zahin
If using libraries or built-in functions is to be avoided then the following code may help:
如果要避免使用库或内置函数,那么以下代码可能会有所帮助:
s = "aaabbc" # sample string
dict_counter = {} # empty dict for holding characters as keys and count as values
for char in s: # traversing the whole string character by character
if not dict_counter or char not in dict_counter.keys(): # Checking whether the dict is
# empty or contains the character
dict_counter.update({char: 1}) # if not then adding the character to dict with count = 1
elif char in dict_counter.keys(): # if the char is already in the dict then update count
dict_counter[char] += 1
for key, val in dict_counter.items(): # Looping over each key and value pair for printing
print(key, val)
Output:
a 3
b 2
c 1
输出:
a 3
b 2
c 1
回答by Swetank
s=input()
t=s.lower()
for i in range(len(s)):
b=t.count(t[i])
print("{} -- {}".format(s[i],b))
回答by Syed Rafay
Another way could be to remove repeated characters and iterate only on the unique characters (by using set()
) and then counting the occurrence of each unique character (by using str.count()
)
另一种方法可能是删除重复字符并仅迭代唯一字符(使用set()
),然后计算每个唯一字符的出现次数(使用str.count()
)
def char_count(string):
freq = {}
for char in set(string):
freq[char] = string.count(char)
return freq
if __name__ == "__main__":
s = "HelloWorldHello"
print(char_count(s))
# Output: {'e': 2, 'o': 3, 'W': 1, 'r': 1, 'd': 1, 'l': 5, 'H': 2}
回答by Stanley
For future references: When you have a list with all the words you want, lets say
wordlist
it's pretty simple
供以后参考:当你有一个包含你想要的所有单词的列表时,让我们说
wordlist
这很简单
for numbers in range(len(wordlist)):
if wordlist[numbers][0] == 'a':
print(wordlist[numbers])
回答by evadeflow
Following up what LMc said, your code was already pretty close to functional, you just needed to post-process the result set to remove 'uninteresting' output. Here's one way to make your code work:
按照 LMc 所说,您的代码已经非常接近功能,您只需要对结果集进行后处理以删除“无趣”的输出。这是使您的代码工作的一种方法:
#!/usr/bin/env python
word = raw_input("Enter a word: ")
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'
]
hits = [
(Alphabet[i], word.count(Alphabet[i]))
for i in range(len(Alphabet))
if word.count(Alphabet[i])
]
for letter, frequency in hits:
print letter.upper(), frequency
But the solution using collections.Counter
is much more elegant/Pythonic.
但是使用的解决方案collections.Counter
更加优雅/Pythonic。