selfteaching / selfteaching/selfteaching-python-camp
Day06 关于使用正则表达式统计汉字字频的问题
Open
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 151
- Forks
- 875
- PR merge metrics
- No merged PRs in 30d
Description
为什么出来的结果里包含了标点符号?
代码如下:
def stats_text_cn(string):
import re #导入正则表达式模块
result_cn_interpunction = re.sub('[^\u4e00-\u9fa5]','',string) #提取中文字符串
string = string.replace(' ','').replace('\n','') #删除空元素与换行元素
list1 = re.split('',string) #将字符串转换为列表list1
dict1 = {} #建立空的字典
for i in list1: #i属于list1中的元素,开始循环
dict1.setdefault(i,list1.count(i)) #将列表中的汉字及汉字的出现次数,分别赋值给dict1的键和值
tup1 = sorted(dict1.items(),key = lambda items:items[1],reverse = True) #将dict1按照value值从大到小排列,并将结果赋值给元祖tup1
return tup1 #返回tup1的值
result = stats_text_cn(text)
print(result)
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the stats_text_cn function shown in the issue and compare the value assigned to result_cn_interpunction with the string passed to re.split. Confirm the expected input and output, then verify that the returned tuples contain only Chinese characters with correct frequency counts.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- data
- Issue type
- Bug
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 38/100