Count the number of Duplicates
Write a function that will return the count of distinct case-insensitive alphabetic characters and numeric digits that occur more than once in the input string. The input string can be assumed to contain only alphabets (both uppercase and lowercase) and numeric digits.
Example
“abcde” -> 0 # no characters repeats more than once
“aabbcde” -> 2 # ‘a’ and ‘b’
“aabBcde” -> 2 # ‘a’ occurs twice and ‘b’ twice (b and B)
“indivisibility” -> 1 # ‘i’ occurs six times
“Indivisibilities” -> 2 # ‘i’ occurs seven times and ‘s’ occurs twice
“aA11” -> 2 # ‘a’ and ‘1’
“ABBA” -> 2 # ‘A’ and ‘B’ each occur twice
这道题让我感受到了Python对于文字处理的强大之处,对于大小写混合,可以使用.lower()和.upper()将其自动切换成全小写/大写
另外可以利用Counter()方法来统计词频,返回的是一个字典类型的对象
import collections
obj = collections.Counter('aabbccc')
print(obj)
#输出:Counter({'c': 3, 'a': 2, 'b': 2})
然后可以利用迭代器来迭代这个obj,obj.items()便是生成了一个字典类型的迭代器,如果用一个参数去遍历他,那么这个参数是把每一个字典作为元组(key:value)输出,如果有2个参数for i,j in obj.items():那么每次遍历的就是{i:j}。
另外还学习了一下迭代器的知识。
字符串,元组,列表,字典,集合等全都是可以迭代的对象,叫做Iterable。但是这些对象不能够直接进行迭代,就是不能直接像字符串for i in str(text):,(如果要究其原因,就是字符串可以使用next()方法无限next,而上面的其他对象很明显是不能无限next的因为他们的长度都是有限的。
总结:只要能用for循环的,都是可迭代对象(字典可以用for循环key)
如果要创建一个可迭代对象,那通常使用iter()方法,或者使用iter_s = s.__iter__()来创建可迭代对象。创建后,初始值为对象的第一个值,可以使用iter_s.next()
获得下一个值
本文介绍了一个Python函数,用于计算输入字符串中出现超过一次的不区分大小写的字母和数字的数量。文章详细解释了如何使用Python内置函数和库实现该功能,并讨论了如何处理大小写敏感性和字符计数。

被折叠的 条评论
为什么被折叠?



