美文网首页
python实现移除一组字符串中出现次数最少的字符

python实现移除一组字符串中出现次数最少的字符

作者: 小云1121 | 来源:发表于2020-04-11 16:30 被阅读0次

思路:

1,遍历字符串中的字符,求出count数存入字典中

2,求出字典中value值最小的key值存入list

3,遍历字符串,输出不在2中list中字符

#! /usr/bin/python

import string

def del_less_str(new_str):

        num_dec={}

        for i in new_str:

                num=0

                for j in new_str:

                        if i is j:

                                num=num+1

                num_dec[i]=num

        a_list=[v for v in num_dec.values()]

        b_list=[key for key,value in num_dec.items() if value == min(a_list)]

        res=''

        for i in new_str:

                if i in b_list:

                        pass

                else:

                        res+=i

        return res

if __name__=="__main__":

        new_str='aabacja121dkjc'

        print(new_str)

        str_dic=del_less_str(new_str)

        print(str_dic)

相关文章

网友评论

      本文标题:python实现移除一组字符串中出现次数最少的字符

      本文链接:https://www.haomeiwen.com/subject/jihumhtx.html