Python程序查找给定字符串中每个字符的出现
给定一个字符串,任务是用Python编写一个程序,打印字符串中每个字符出现的次数。
Python中有多种方法,我们可以完成这项任务。让我们讨论其中的几个。
方法 #1:使用set()
+ count()
遍历设置的转换字符串并获取原始字符串中每个字符的计数。
# Python3 code to program to find occurrence
# to each character in given string
# initializing string
inp_str = "GeeksforGeeks"
# using set() + count() to get count
# of each element in string
out = {x : inp_str.count(x) for x in set(inp_str )}
# printing result
print ("Occurrence of all characters in GeeksforGeeks is :\n "+ str(out))
输出:
Occurrence of all characters in GeeksforGeeks is :
{'o': 1, 'r': 1, 'e': 4, 's': 2, 'f': 1, 'G': 2, 'k': 2}
方法#2:使用字典
# Python3 code to program to find occurrence
# to each character in given string
# initializing string
inp_str = "GeeksforGeeks"
# frequency dictionary
freq = {}
for ele in inp_str:
if ele in freq:
freq[ele] += 1
else:
freq[ele] = 1
# printing result
print ("Occurrence of all characters in GeeksforGeeks is :\n "+ str(freq))
输出:
Occurrence of all characters in GeeksforGeeks is :
{'e': 4, 'r': 1, 'o': 1, 'f': 1, 'G': 2, 's': 2, 'k': 2}
方法#3:使用collections
# Python3 code to program to find occurrence
# to each character in given string
from collections import Counter
# initializing string
in_str = "GeeksforGeeks"
# using collections.Counter() to get
# count of each element in string
oup = Counter(in_str)
# printing result
print ("Occurrence of all characters in GeeksforGeeks is :\n "+ str(oup))
输出:
Occurrence of all characters in GeeksforGeeks is :
Counter({'e': 4, 's': 2, 'G': 2, 'k': 2, 'f': 1, 'r': 1, 'o': 1})