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( ...
随机推荐
- 【Leetcode_easy】941. Valid Mountain Array
problem 941. Valid Mountain Array solution: class Solution { public: bool validMountainArray(vector& ...
- Red Team远程控制软件
开源远程管理控制 https://github.com/malwaredllc/byob 僵尸网络生成框架 https://github.com/panda-re/lava 大规模向程序中植入恶意程序 ...
- Java 中的并发工具类
Java 中的并发工具类 CountDownLatch public class JoinCountDownLatchTest { public static void main(String[] a ...
- LeetCode 172. 阶乘后的零(Factorial Trailing Zeroes)
172. 阶乘后的零 172. Factorial Trailing Zeroes 题目描述 给定一个整数 n,返回 n! 结果尾数中零的数量. LeetCode172. Factorial Trai ...
- GraphHopper-初识
GraphHopper GraphHopper is a fast and Open Source road routing engine. Is fast and memory efficie ...
- 实现不同分辨率、不同浏览器下高度自适应、iframe高度自适应
html: <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <tit ...
- Logstash配置安装
logstash配置 http.host: xpack.monitoring.enabled: true xpack.monitoring.elasticsearch.username:"l ...
- EXCEL 快捷键大全
1.ctrl+tab :快速切换已打开的excel/ppt/word同样适用 2.shift+右上角的× :关闭所有excel/ppt/word 3.ctrl+d:快速复制非文本目标,对于文本是删除 ...
- Maven学习存档(2)——settings.xml配置
二.settings.xml配置 2.1 原文 <?xml version="1.0" encoding="UTF-8"?> <!-- Lic ...
- spring整合MQ
---恢复内容开始--- 一. 导入依赖 <dependencies> <!-- ActiveMQ客户端完整jar包依赖 --> <dependency> < ...