import collections import string with open(‘cipher.txt’) as f: f = f.read().replace(‘ ‘, ‘’).replace(‘\n’,’‘).lower() f = f.strip(string.punctuation)
cnt = collections.Counter(f.replace(' ', '')) for letter in sorted(cnt): print(letter, cnt[letter])
我如何去除标点符号!我不知道该在哪里放置那条线?有人可以修改我的代码以删除字母以外的所有内容吗?谢谢
使用str.translate()删除代码点; None删除任何映射到的代码点:
str.translate()
None
remove = dict.fromkeys(map(ord, '\n ' + string.punctuation)) f.translate(remove)
使用dict.fromkeys()class方法可以轻松创建将所有键映射到的字典None。
dict.fromkeys()
演示:
>>> import string >>> remove = dict.fromkeys(map(ord, '\n ' + string.punctuation)) >>> sample = 'The quick brown fox, like, totally jumped, man!' >>> sample.translate(remove) 'Thequickbrownfoxliketotallyjumpedman'
调整为您的代码:
remove = dict.fromkeys(map(ord, '\n ' + string.punctuation)) with open('cipher.txt') as inputfile: f = inputfile.read().translate(remove)