开干
1、python类型属性、函数考点
2、计算40个大题的代码中函数出现次数。
import jieba
f = open("新建文本文档.txt", "r", encoding="utf-8")
lines = f.readlines()
f.close()
d = {}
for line in lines:
wordList = jieba.lcut(line) # 用结巴分词,对每行内容进行分词
for word in wordList:
if len(word) < 3:
continue
else:
d[word] = d.get(word, 0) + 1
ls = list(d.items())
ls.sort(key=lambda x: x[1], reverse=True) # 按照词频由高到低排序
f = open('out2.txt', 'w', encoding="utf-8")
for i in range(len(ls)):
f.write('{}:{}\n'.format(ls[i][0], ls[i][1]))
f.close()
结果
line:156 #行,遍历文件为行
for:140 #for i in rang,for i in X 循环遍历
txt:121 #大量文本
open:84 #打开大量文本格式
print:75 #打印测试
close:71 #关闭文件 f.close()
encoding:59 #encoding ="utf-8"以UTF-8格式读取文件
utf:56 #encoding ="utf-8"以UTF-8格式读取文件
format:49 #格式化字符串
split:39 #line.split(",")以逗号或者空格分隔字符串
append:38 #列表追加字符串
len:38 #长度
range:37 #for in range 循环遍历
row:36 #for row in ls:循环遍历行
write:30 #f.write('\n') # 换行写入
jieba:29 #结巴分词库
key:27 #字典、值
replace:26 #替换字符串内容
readlines:25 #读取文件lines = f.readlines()
lines:23 #读取文件lines = f.readlines()
count:22 #计数
list:21 #列表
get:20 #words[w] = words.get(w,0)+1,循环遍历后字典累加计数
else:20 #if else
word:20
flag:19
True:19
strip:18
cnt:16
import:16
item:16
reverse:16
items:16
平均值:16
join:15
sort:15
lambda:15
res:15
lcut:14
input:14
end:14
words:14
with:14
def:14
...:13
alt:13
字符串:13
read:12
data:12
最大值:12
elif:12
总成绩:12
numbers:11
sum:11
urls:10
csv:10
file:10
int:10