Python-pythonで文字列の各文字の出現回数を統計するにはどうすればいいですか?
702 ワード
def calculate_character_num(s):
'''This function calculate each character(from A-z) number from str.'''
#
s =list( ''.join(s.split()))
#
s1 = set(s)
#
l = list(s1)
t ={}
for a in range(len(l)):
num = 0
# reversed() , pop() , 。
# pop() , for 。
for i in reversed(range(len(s))):
if l[a] == s[i]:
num = num + 1
s.pop(i)
t[l[a]] = str(num)
return t
if __name__ == '__main__':
str1 = 'hello world'
t = calculate_character_num(str1)
print(t)