python统计字符串中单个字符出现次数

str = 'skdaskerkjsalkj'
list = []

for i in  range(len(str)):
    list.append(str[i])
list2 = sorted(list)
print(list2)

for i in range(len(list)):
    flag2 = False
    st = list2[i]
    k = i - 1
    #是不是第一个
    while k >= 0:
        if (list2[k] == st):
            flag2 = False
            break
        else:
            flag2 = True
        k -= 1
    #向后查询
    if (flag2 or i==0):
        count = 0
        for j in range(i, len(list)):
            if (list2[j] == st):
                count += 1
        print(st,count)
去重
li_one =[1,2,1,2,3,5,4,3,5,7,8]
list2 = sorted(li_one)
n = 0
list3 = []
for i in range(len(li_one)):
    flag2 = False
    st = list2[i]
    k = i - 1
    #是不是第一个
    while k >= 0:
        if (list2[k] == st):
            flag2 = False
            break
        else:
            flag2 = True
        k -= 1

    if (flag2 or i==0):
       list3.append(st)
       n +=1
print(list3)
posted @ 2024-11-06 21:40  基础狗  阅读(21)  评论(0编辑  收藏  举报