【计算机二级Python】易忘知识点总结
【摘要】
二级备赛系列博文
【计算机二级Python】客观题(总结版)【计算机二级Python】主观题(总结版)【计算机二级Python】易忘知识点总结【计算机二级Python】阶段性总结版
PYTHON二...
二级备赛系列博文
PYTHON二级备赛资料:链接:https://pan.baidu.com/s/1he8SAvb2j9JjtGtTgk4_HA
,提取码:ei21
-
列表的操作方法
-
random库
-
字典
-
文件
-
format中的格式是:内容 -> 填充 -> 对齐 -> 宽度 为固定格式。同时别忘了最外面有双引号和大话括号,以及冒号,
"{: }".format()...
-
for 循环写作一行的方法
print("True" if type(eval("123")) == type(1) else "False"
注意,无需加冒号
#基础版本的词频统计
fi = open("1.txt", "r", encoding = "utf-8")
fo = open("2.txt", "w", encoding = "utf-8")
txt = fi.read()
d = {}
for c in fi:
d[c] = d.get(c, 0) + 1
del d[' ']
del d['\n']
ls = []
for key in d:
ls.append("{}:{}".format(key, d[key]))
fo.write(",".join(ls))
fi.close()
fo.close()
#中级版本的词频统计
#加入jieba库后的词频统计
import jieba
fi = open("1.txt", "r", encoding = "utf-8")
fo = open("2.txt", "w", encoding = "utf-8")
t = fi.read()
txt = jieba.lcut(t)
d = {}
for c in txt:
d[c] = d.get(c, 0) + 1
del d[' ']
del d['\n']
ls = []
for key in d:
ls.append("{}:{}".format(key, d[key]))
fo.write(",".join(ls))
fi.close()
fo.close()
#高级版本的词频统计
#高频词提取并排序
fi = open("1.txt", "r", encoding = "utf-8")
fo = open("2.txt", "w", encoding = "utf-8")
txt = fi.read()
d = {}
for c in txt:
d[c] = d.get(c, 0) + 1
del d[' ']
del d['\n']
ls = list(d.items())
ls.sort(key=lambda x:x[1], reverse=True)
for i in range(100)
ls[i] = "{}:{}".format(ls[i][0], ls[i][1])
fo.write(",".join(ls[:100]))
fi.close()
fo.close()
#特高级版本
#在上述统计高频词并排序后,比较两个文件中相同的字符
def getList(name):
fi = open(name+".txt", "r", encoding = "utf-8")
words = fi.read().split(',')
for i in range(len(words)):
words[i] = words[i].split(':')[0]
fi.close()
return words
def main():
fo = open("out.txt", "w", encoding="utf-8")
ls1 = getList("1")
ls2 = getList("2")
for c in ls1:
for c in ls2:
ls3.append(c)
fo.write(",".join(ls3))
fo.close()
main()
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
文章来源: recclay.blog.csdn.net,作者:ReCclay,版权归原作者所有,如需转载,请联系作者。
原文链接:recclay.blog.csdn.net/article/details/88883887
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)