用正则表达式清除各类符号

一篇文章,先转为单词为元素的列表。

分解时遇到的第一个问题,就是如果去除各类标点符号。 

import re 

 line='asdf fjdk;;;; s afred,,fjek.asdf, foo^ sdkk'

 re.split(r'[;^,.\s]*',line) # 表示后面的字符串没有转义符。【这里面是要作为分隔的各类符号】,外面的*表示重复也算。

运算结果,['asdf', 'fjdk', 'afred', 'fjek', 'asdf', 'foo', 'sdkk']

在得到分解LIST后,就可以进行单词分析了。要用到Counter工具。

words = [

'look', 'into', 'my', 'eyes', 'look', 'into', 'my', 'eyes',

'the', 'eyes', 'the', 'eyes', 'the', 'eyes', 'not', 'around', 'the',

'eyes', "don't", 'look', 'around', 'the', 'eyes', 'look', 'into',

'my', 'eyes', "you're", 'under'

]

from collections import Counter

word_counts = Counter(words)

top_three = word_counts.most_common(3)

print(top_three)

# Outputs [('eyes', 8), ('the', 5), ('look', 4)]

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值