1:下载一首英文的歌词或文章

love story-taylor swift
we were both young when i first saw you
i close my eyes and the flashback starts
i'm standing there on a balcony in summer air
see the lights, see the party, the ball gowns
see you make your way through the crowd
and say hello, little did i know
that you were romeo, you were throwing pebbles
and my daddy said stay away from juliet
and i was crying on the staircase, begging you please don't go
and i said
romeo take me somewhere we can be alone
i'll be waiting, all there's left to do is run
you'll be the prince and i'll be this princess
it's a love story
baby, just say yes
so i sneak out to the garden to see you
we keep quiet 'cause we're dead if they knew
so close your eyes, escape this town for a little while
oh, oh, oh
'cause you were romeo, i was a scarlet letter
and my daddy said stay away from juliet
but you were everything to me, i was begging you please don't go
and i said
romeo take me somewhere we can be alone
i'll be waiting, all there's left to do is run
you'll be the prince and i'll be the princess
it's a love story
baby, just say yes
romeo save me try to tell me how it feels
this might be stupid boy, but its so real
don't be afraid now we'll get out of this mess
it's a love story
baby, just say yes
i got tired of waiting wondering if you were ever coming around
my faith in you is better
when i met you on the outskirts of town
and i said
romeo save me ive been feeling so alone
ill keep waiting for you but you never come
is this in my head, i don't know what to think
he fell to the ground and pulled out a ring
and said
marry me juliet you'll never have to be alone
i love you and that's all i really know
i talked to your dad you'll pick out a white dress
it's a love story
baby, just say yes
oh, oh, oh
we were both young when i first saw you

2:将所有,.?!’:等分隔符全部替换为空格

sep=''';,.?!'''for i in sep:

    str=str.replace(i,' ')

3.将所有大写转换为小写
str=str.lower()

4:生成单词列表
   str_list=str.split()

5:
str_list=str.split()
print(str_list) str_dict={}
for i in str_list:
str_dict[i]=str_dict.get(i,0)+1
#去掉不要的单词
for w in str:
del (str_dict)
print(w,str_dict[w])
6:排序
strList=list(str_dict.items())
strList.sort(key=lambda x:x[1] ,reverse=True)
7:排除语法型词汇,代词、冠词、连词
exclude={'the','top','is','while','when','why'}
for i in exclude:
del(str_dict) 8:输出词频最大TOP20
for i in range(20):
print(strList[i]) 9:将分析对象存为utf-8编码的文件,通过文件读取的方式获得词频分析内容。
file=open('shuihuzhuan.txt','r',encoding='utf-8')
myarticle=file.read()

二、中文词频统计,下载一长篇中文文章。

代码如下:

import jieba
file=open("hh.txt","r",encoding='utf-8')
mynotes=file.read()
file.close(); sep = ''':。,?!;∶ ...“”'''
for i in sep:
mynotes = mynotes.replace(i, ' '); mynotes_list = list(jieba.cut(mynotes)); exclude =[' ','\n','你','我','他','和','但','了','的','来','是','去','在','上','高'] mynotes_dict={}
for w in mynotes_list:
mynotes_dict[w] = mynotes_dict.get(w,0)+1

//取出指定内容
for w in exclude:
del (mynotes_dict[w]); for w in mynotes_dict:
print(w,mynotes_dict[w]) //排序
dictList = list(mynotes_dict.items())
dictList.sort(key=lambda x:x[1],reverse=True);
print(dictList) //输出20的文本内容
for i in range(20):
print(dictList[i])

//把频率多于20的输出到文本
outfile = open("mytop20.txt","a")
for i in range(20):
outfile.write(dictList[i][0]+" "+str(dictList[i][1])+"\n")
outfile.close();

  

python函数练习的更多相关文章

  1. python 函数之day3

    一 函数的语法及特性 什么是函数? 定义:函数是一个功能通过一组语句的集合,由名字(函数名)将其封装起来的代码块,要想执行这个函数,只要调用其函数名即可. 特性: 减少重复代码 使程序变的可扩展 使程 ...

  2. Python函数作用域的查找顺序

    函数作用域的LEGB顺序 1.什么是LEGB? L:local 函数内部作用域 E:enclosing 函数内部与内嵌函数之间 G:global 全局作用域 B:build-in 内置作用域 2.它们 ...

  3. Python函数讲解

    Python函数

  4. Python函数信息

    Python函数func的信息可以通过func.func_*和func.func_code来获取 一.先看看它们的应用吧: 1.获取原函数名称: 1 >>> def yes():pa ...

  5. Python函数参数默认值的陷阱和原理深究"

    本文将介绍使用mutable对象作为Python函数参数默认值潜在的危害,以及其实现原理和设计目的 本博客已经迁移至: http://cenalulu.github.io/ 本篇博文已经迁移,阅读全文 ...

  6. Python开发【第四章】:Python函数剖析

    一.Python函数剖析 1.函数的调用顺序 #!/usr/bin/env python # -*- coding:utf-8 -*- #-Author-Lian #函数错误的调用方式 def fun ...

  7. Python函数解析

    对于Python的函数,我们需要记住的是: 1. 函数的默认返回值是None. 2. python是一个自上而下逐行解释并执行的语言.因此,函数的定义必须在函数被调用之前.同名的函数,后定义的会覆盖前 ...

  8. Python入门笔记(18):Python函数(1):基础部分

    一.什么是函数.方法.过程 推荐阅读:http://www.cnblogs.com/snandy/archive/2011/08/29/2153871.html 一般程序设计语言包含两种基本的抽象:过 ...

  9. Python函数1

    Python 函数命令的使用 想想我们之前数学中学到的函数,首先我们需要定义一个函数,例如f(x)=x, 当x输入任意数的时候,f(x)都能输出和x相等的数值. 那么在Python中是如何实现的呢? ...

  10. python函数传参是传值还是传引用?

    首先还是应该科普下函数参数传递机制,传值和传引用是什么意思? 函数参数传递机制问题在本质上是调用函数(过程)和被调用函数(过程)在调用发生时进行通信的方法问题.基本的参数传递机制有两种:值传递和引用传 ...

随机推荐

  1. Simple Windows Service in C++

    本文是来自CodeProject中的一篇名为Simple Windows Service in C++的译文,原文地址为:https://www.codeproject.com/Articles/49 ...

  2. 普通Apache的安装与卸载

    Apache安装与卸载ctrl+F快捷查找 1.下载apache 64位解压 官网:http://httpd.apache.org/ 文件使用记事本或者sublime2.修改 打开apache目录下的 ...

  3. SpringCloud微服务小白入门之Eureka注册中心和服务中心搭建示例

    一.注册中心配置文件 代码复制区域: spring: application: name: spring-cloud-server server: port: 7000 eureka: instanc ...

  4. oracle 正则表达的使用

    最近遇到有个项目,需要根据文件存储的根目录地址来判断是在云端获取,还是本地获取, 先看下具体有几个不同的根目录: , , 'i') from pmc.designmaterial d 去重关键字:di ...

  5. 程序员的专属微信公众号编辑器:定制 Markdown 转 HTML

    效果(Gif) 点击我查看 仓库地址 https://github.com/cdk8s/cdk8s-markdown-to-html https://gitee.com/cdk8s/cdk8s-mar ...

  6. Redis的常用命令与Java整合及高级应用篇

    一,redis是什么? ​ 首先数据库分为关系型数据库和非关系型数据库,关系型数据库是采用关系模型来组织数据的数据库,简单来说就是二维表格模型,同时保证事务的一致性. ​ 相反非关系型数据库采用key ...

  7. Java中不得不谈的值传递和地址传递

    个人的一些认识,希望能对初学Java的你,或者困惑于方法参数传递的你祈祷一丝帮助! 下面是一些作者的个人观点,如果有错,欢迎各位大牛指出错误,灰常感谢您的观看与支持... -------------- ...

  8. Mac忘记MySQL root密码解决方法(亲测有效)

    重置MySQL root密码 重置MySQL root用户的密码: 1)新建一个文本文件sql.txt,写入修改用户密码的SQL语句. MySQL 5.7.6及更高版本写这句: ALTER USER ...

  9. Flink快速入门--安装与示例运行

    flink是一款开源的大数据流式处理框架,他可以同时批处理和流处理,具有容错性.高吞吐.低延迟等优势,本文简述flink在windows和linux中安装步骤,和示例程序的运行. 首先要想运行Flin ...

  10. jqGrid根据数据动态设置rowList

    superme.rowList = [10,20,50,100]; superme.rowNum = 20; 全局属性 loadComplete : function (data) { if(data ...