python 统计英文单词出现频率
程序员文章站
2024-02-23 23:26:10
...
# coding = utf-8
wordstring = '''I never saw a Moor-I never saw the Sea.
Yet know I how the Heather looks.
And what a Billow be.
I never spoke with God.
Nor visited in Heaven.
Yet certain am I of the spot.
As if the Checks were given.'''
wordstring = wordstring.replace('.', '')
wordlist = wordstring.split()
wordfreq = []
for w in wordlist:
wordfreq.append(wordlist.count(w))
d = dict(zip(wordlist,wordfreq))
print(d)
{'I': 4, 'never': 3, 'saw': 2, 'a': 2, 'Moor-I': 1, 'the': 4, 'Sea': 1, 'Yet': 2, 'know': 1, 'how': 1, 'Heather': 1, 'looks': 1, 'And': 1, 'what': 1, 'Billow': 1, 'be': 1, 'spoke': 1, 'with': 1, 'God': 1, 'Nor': 1, 'visited': 1, 'in': 1, 'Heaven': 1, 'certain': 1, 'am': 1, 'of': 1, 'spot': 1, 'As': 1, 'if': 1, 'Checks': 1, 'were': 1, 'given': 1}
上一篇: sort、uniq、cut命令操作
下一篇: Linux-grep命令学习