# encoding=utf-8
# @Time :2021/11/5 19:38
# @Author :大壮
# @File :count.py
mylist = 'aslkd fhkl asjdfhials udhfiawu efsudhf'
# 定义空字典
count = {}
# 遍历字符串
for i in mylist:
# 第一次查询到,计数:1
if i not in count:
count[i] = 1
else: # 再次查询到相同字符,计数+1
count[i] += 1
print(count)
# 结果
# {'a': 4, 's': 4, 'l': 3, 'k': 2, 'd': 4, ' ': 6, 'f': 5, 'h': 4, 'j': 1, 'i': 2, 'u': 3, 'w': 1, 'e': 1}
10-18
687
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)