# -*- 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文件写入的更多相关文章

  1. python统计元素重复次数

    python统计元素重复次数 # !/usr/bin/python3.4 # -*- coding: utf-8 -*- from collections import Counter arr = [ ...

  2. python 统计单词出现次数

    #use python3.6 import re from collections import Counter FILESOURCE = './abc.txt' def getMostCommonW ...

  3. Python 网络爬虫 010 (高级功能) 解析 robots.txt 文件

    解析 robots.txt 文件 使用的系统:Windows 10 64位 Python 语言版本:Python 2.7.10 V 使用的编程 Python 的集成开发环境:PyCharm 2016 ...

  4. ----关于统计字符出现次数的JS循环以及indesxOf函数----

    以下将会通过JS循环判断字符“banana”出现次数 以及调用indexOf中的函数来实现统计   <!DOCTYPE html> <html> <body> &l ...

  5. JS-使用indexof来统计字符出现次数

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  6. js统计字符出现次数

    var s = "The rain in Spain falls rain mainly in the rain plain"; var reg = new RegExp(&quo ...

  7. 【python基础语法】模块和包管理,文件的操作(第8天课堂笔记)

    ''' 模块和包管理 模块和包的定义: 模块:模块是一个Python文件,以.py结尾,包含了Python对象定义和Python语句 包:Python中的包就是一个包含__init__.py文件的目录 ...

  8. 转载:python生成以及打开json、csv和txt文件

    原文地址:https://blog.csdn.net/weixin_42555131/article/details/82012642 生成txt文件: mesg = "hello worl ...

  9. python当前工作文件夹中创建空的.txt文件

    import os def new_txt(): a1='实线' b = os.getcwd() + '\\fazhandadao_test_txt\\' if not os.path.exists( ...

随机推荐

  1. Linux脚本检测当前用户是否为root用户

    #/bin/bash if [ $UID -ne 0 ]; then echo Non root user. Please run as root. else echo Root user fi

  2. Amazon MWS Scratchpad

    https://mws.amazonservices.com/scratchpad/index.html Use this page to test Amazon MWS API request an ...

  3. pt-kill MySQL会话杀灭神器

    废话不多说,直接上例子: pt-kill --host=127.0.0.1 --user=xxx --password=xxxxxx --port=xxxx --busy-time 10 --matc ...

  4. C++ 宏和模板简介

    参考<21天学通C++>第14章节,对C++中的宏和模板进行了学习,总结起来其主要内容如下: (1) 预处理器简介 (2) 关键字#define与宏 (3) 模板简介 (4) 如何编写函数 ...

  5. ROS中的通信机制

    http://www.ros.org/core-components/ Communications Infrastructure At the lowest level, ROS offers a ...

  6. DAO工具类的封装源码

    详细源码见下表,绝对原创,转载请注明出处! package com.ydj.util; import java.sql.Connection; import java.sql.PreparedStat ...

  7. synchronized的不足与redis分布式锁的使用

    这里是一个简单模拟秒杀的逻辑,stock和orders为两个Map,分别模拟库存表和订单表 public void orderProductMockDiffUser(String productId) ...

  8. Windows10+Anaconda+PyTorch(cpu版本)环境搭建

    1.安装Anaconda,具体参考网上相关教程 2.安装PyTorch 2.1 在Anaconda自带的Anaconda Prompt中创建名为PyTorch的虚拟环境[conda create -- ...

  9. 剑指offer41:所有和为S的连续正数序列,例如,有多少种连续的正数序列的和为100

    1 题目描述 小明很喜欢数学,有一天他在做数学作业时,要求计算出9~16的和,他马上就写出了正确答案是100.但是他并不满足于此,他在想究竟有多少种连续的正数序列的和为100(至少包括两个数).没多久 ...

  10. PAT甲级题分类汇编——杂项

    本文为PAT甲级分类汇编系列文章. 集合.散列.数学.算法,这几类的题目都比较少,放到一起讲. 题号 标题 分数 大意 类型 1063 Set Similarity 25 集合相似度 集合 1067 ...