刷题codewars 练习-1
Count characters in your string:计算字符串中单字(6kyu)
介绍:
主要思想是计算字符串中所有出现的字符。如果您有类似的字符串aba,
则结果应为{‘a’: 2, ‘b’: 1}。
如果字符串为空怎么办?然后,结果应为空对象文字{}。
自己练习:
def count(string):
result={}
for i in string:
result[i]=string.count(i)
return out
参考答案-1
def count(string):
return {i: string.count(i) for i in string}
参考答案-2
from collections import Counter as count