Python 定义一个函数,检查字符里面是否有重复的字母,并计算重复字母的个数(忽略大小写)

方法一:

def duplicated_count(text):
    result=[]
    text=text.lower()
    for i in text:
        if text.count(i) > 1 and i not in result:
            result.append(i)
    return len(result)

 调用函数:

duplicated_count('I love you very much!')

返回:

 

方法二:

def dup_counts(s):
    return len([c for c in set(s.lower()) if s.lower().count(c)>1])

调用函数:

dup_counts('I Love you very much!')

返回:

posted @ 2019-05-20 22:59  赏尔  阅读(1084)  评论(0编辑  收藏  举报