Python统计字符出现次数(Counter包)以及txt文件写入
# -*- coding: utf-8 -*-
#spyder (python 3.7)
1. 统计字符(可以在jieba分词之后使用)
from collections import Counter
from operator import itemgetter # txt_list可以写成函数参数进行导入
txt_list = ['千古','人间','人间','龙','龙','龙','哈哈哈','人才','千古','千古']
c = Counter()
for x in txt_list:
if len(x) >= 1:
if x == '\r\n' or x == '\n' or x == ' ':
continue
else:
c[x] += 1
print('常用词频统计结果: \n')
for (k, v) in c.most_common(4): #打印排名前四位
print('%s%s %s %d' % (' ' * (3 ), k, '*' * 3, v)) # 按照词频数从大到小打印
d = sorted(c.items(),key=itemgetter(1),reverse = True)
for ss,tt in d:
out_words=ss + '\t' + str(tt)
print(out_words)
2. 多次覆盖,循环写入文件
#写入文件,多次写入,后一次覆盖前一次,但是out_words本身是在叠加的
#即:第一次写入的是:千古\t3\n;第二次写入的是:千古\t3\n龙\t3\n,覆盖上一次的数据;
#第三次是:千古\t3\n龙\t3\n人间\t2\n,继续覆盖上一次的数据
out_words = ''
for ss,tt in d:
out_words=out_words + ss + '\t' + str(tt) + '\n'
with open(r".\sss.txt", "w",encoding='utf-8') as f:
f.write(out_words+'\n')
比如,循环两次的结果是:
3. 一次性写入文件,中间不会覆盖和多次写入;但是如果重复运行代码,则会覆盖之前的全部内容,一次性重新写入所有新内容
out_words = ''
for ss,tt in d:
out_words=out_words + ss + '\t' + str(tt) + '\n'
with open(r".\ttt.txt", "w",encoding='utf-8') as f:
f.write(out_words+'\n')
Python统计字符出现次数(Counter包)以及txt文件写入的更多相关文章
- python统计元素重复次数
python统计元素重复次数 # !/usr/bin/python3.4 # -*- coding: utf-8 -*- from collections import Counter arr = [ ...
- python 统计单词出现次数
#use python3.6 import re from collections import Counter FILESOURCE = './abc.txt' def getMostCommonW ...
- Python 网络爬虫 010 (高级功能) 解析 robots.txt 文件
解析 robots.txt 文件 使用的系统:Windows 10 64位 Python 语言版本:Python 2.7.10 V 使用的编程 Python 的集成开发环境:PyCharm 2016 ...
- ----关于统计字符出现次数的JS循环以及indesxOf函数----
以下将会通过JS循环判断字符“banana”出现次数 以及调用indexOf中的函数来实现统计 <!DOCTYPE html> <html> <body> &l ...
- JS-使用indexof来统计字符出现次数
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- js统计字符出现次数
var s = "The rain in Spain falls rain mainly in the rain plain"; var reg = new RegExp(&quo ...
- 【python基础语法】模块和包管理,文件的操作(第8天课堂笔记)
''' 模块和包管理 模块和包的定义: 模块:模块是一个Python文件,以.py结尾,包含了Python对象定义和Python语句 包:Python中的包就是一个包含__init__.py文件的目录 ...
- 转载:python生成以及打开json、csv和txt文件
原文地址:https://blog.csdn.net/weixin_42555131/article/details/82012642 生成txt文件: mesg = "hello worl ...
- python当前工作文件夹中创建空的.txt文件
import os def new_txt(): a1='实线' b = os.getcwd() + '\\fazhandadao_test_txt\\' if not os.path.exists( ...
随机推荐
- 对 Watchbog Botnet 渗透过程和 Payload 的分析
漏洞利用 CVE-2018-1000861 https://jenkins.io/security/advisory/2018-12-05/ Watchbog在做什么? Watchbog僵尸网络为其所 ...
- 【C# 开发技巧】 c#窗体关于调试界面和运行界面不一样的原因之一
如下图所示: 原因是因为主窗体属性AutoScaleMode设置为:Font了: 按自己需求将AutoScaleMode设置修改即可. 另外一个可能是系统显示-缩放与布局-要调整到100% 如下图:
- HTTP权威指南-URL与资源
URL与资源 URL是URI的子集 方案(http),主机(www.baidu.com),路径(/home/logo.png) 方案,其实有很多,HTTP.HTTPS.FTP,SMTP等等. http ...
- 随记sqlserver学习笔记(一)
create database testuse test --部门表create table department( dept_id int not null identity primary key ...
- Python--类的定义与使用
转载自https://www.liaoxuefeng.com/wiki/001374738125095c955c1e6d8bb493182103fac9270762a000/0013868200407 ...
- 【并发】8、借助redis 实现多线程生产消费阻塞队列
顾名思义这个就是再消费的时候,不是之前的那哥用yield进行线程切换的操作,而是用线程等待阻塞的方式去执行,说实话我感觉效率不一定有之前那个好, 因为我对这种阻塞队列使用的时候,之前有发现阻塞队列,塞 ...
- 谷歌chrome浏览器提示“喔唷 崩溃啦”的解决方案
原因分析:有可能是注册列表被一些卫士类优化工具或杀毒软件优化了. 解决方案:1. 卸载谷歌浏览器. ①开始→控制面板→添加或删除程序→找到谷歌浏览器卸载(卸载时勾选删除数据) ② 进入注册列表删除谷歌 ...
- 网页修改<title ></title >标签内容
document.title = 'xxxxxx';
- 2019牛客多校九 I. KM and M (类欧几里得)
大意: 给定$N,M$, 求$\sum\limits_{K=1}^N \text{(KM)&M}$ 考虑第$i$位的贡献, 显然为$\lfloor\frac{KM}{2^i}\rfloor$为 ...
- Markdown试试
from os import time print("haha") from os import time print("haha") time.time()! ...