python 统计英文单词出现频率

该博客介绍了一个简单的Python脚本,用于统计一段英文诗歌中各单词出现的频率。通过对文本进行预处理,去除标点符号,并将文本拆分为单词列表,接着统计每个单词出现的次数并打印出来。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

# 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}

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值