Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我试图在忽略大小写、空格和特殊字符的字符串中查找每个字母的出现次数。做这个的最好方式是什么。
例如:
i/p: ABCccCDde :)! f o/p: A=1, B=1, C=4, D=2, E=1
我试过了
abc = Counter(line.rstrip('\n'))
而且,defaultdict但他们不忽略大小写。我还需要在不花费太多时间的情况下清除特殊字符
defaultdict
尝试
>>> abc = 'ABCccCDde :)! f' >>> from collections import Counter >>> Counter(c for c in abc.upper() if c.isalpha()) Counter({'C': 4, 'D': 2, 'A': 1, 'B': 1, 'E': 1, 'F': 1})