python函数练习
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函数练习的更多相关文章
- python 函数之day3
一 函数的语法及特性 什么是函数? 定义:函数是一个功能通过一组语句的集合,由名字(函数名)将其封装起来的代码块,要想执行这个函数,只要调用其函数名即可. 特性: 减少重复代码 使程序变的可扩展 使程 ...
- Python函数作用域的查找顺序
函数作用域的LEGB顺序 1.什么是LEGB? L:local 函数内部作用域 E:enclosing 函数内部与内嵌函数之间 G:global 全局作用域 B:build-in 内置作用域 2.它们 ...
- Python函数讲解
Python函数
- Python函数信息
Python函数func的信息可以通过func.func_*和func.func_code来获取 一.先看看它们的应用吧: 1.获取原函数名称: 1 >>> def yes():pa ...
- Python函数参数默认值的陷阱和原理深究"
本文将介绍使用mutable对象作为Python函数参数默认值潜在的危害,以及其实现原理和设计目的 本博客已经迁移至: http://cenalulu.github.io/ 本篇博文已经迁移,阅读全文 ...
- Python开发【第四章】:Python函数剖析
一.Python函数剖析 1.函数的调用顺序 #!/usr/bin/env python # -*- coding:utf-8 -*- #-Author-Lian #函数错误的调用方式 def fun ...
- Python函数解析
对于Python的函数,我们需要记住的是: 1. 函数的默认返回值是None. 2. python是一个自上而下逐行解释并执行的语言.因此,函数的定义必须在函数被调用之前.同名的函数,后定义的会覆盖前 ...
- Python入门笔记(18):Python函数(1):基础部分
一.什么是函数.方法.过程 推荐阅读:http://www.cnblogs.com/snandy/archive/2011/08/29/2153871.html 一般程序设计语言包含两种基本的抽象:过 ...
- Python函数1
Python 函数命令的使用 想想我们之前数学中学到的函数,首先我们需要定义一个函数,例如f(x)=x, 当x输入任意数的时候,f(x)都能输出和x相等的数值. 那么在Python中是如何实现的呢? ...
- python函数传参是传值还是传引用?
首先还是应该科普下函数参数传递机制,传值和传引用是什么意思? 函数参数传递机制问题在本质上是调用函数(过程)和被调用函数(过程)在调用发生时进行通信的方法问题.基本的参数传递机制有两种:值传递和引用传 ...
随机推荐
- API开发之接口安全(三)----sign有效时间
之前生成的sign和校验sign我们已经完全掌握了.但是仅仅凭借这样的sign是无法满足我们的需求的,如果一个黑客通过抓包抓到你的数据 他可以去修改你的header为这样的 body为那样的 也是可以 ...
- 依赖注入在 dotnet core 中实现与使用:1 基本概念
关于 Microsoft Extension: DependencyInjection 的介绍已经很多,但是多数偏重于实现原理和一些特定的实现场景.作为 dotnet core 的核心基石,这里准备全 ...
- ajax中的后台返回数据data的意义
- java JVM原理讲解和调优和gc
- getline()与get()(c++学习笔记)
istream中的类(如cin)提供了一些面向行的类成员函数:getline()和get() 1.getline()函数 读取整行,使用回车键输入的换行符来确定输入结尾. 调用方法:cin.getli ...
- Excel VBA 在保留原单元格数据的情况下,将计算的百分比加在后面
算的是红框占绿框的百分比 难点在保留原数据的情况下,把百分比加在后面.通过公式我是不会,但程序实现也不难. 先在Excel中的开发工具中打开visual basic,或者用宏也可以 导入代码文件,代码 ...
- Python爬虫爬取全书网小说,程序源码+程序详细分析
Python爬虫爬取全书网小说教程 第一步:打开谷歌浏览器,搜索全书网,然后再点击你想下载的小说,进入图一页面后点击F12选择Network,如果没有内容按F5刷新一下 点击Network之后出现如下 ...
- three.js实现球体地球城市模拟迁徙
概况如下:1.SphereGeometry实现自转的地球:2.THREE.ImageUtils.loadTexture加载地图贴图材质:3.THREE.Math.degToRad,Math.sin,M ...
- Django 项目创建到启动(最全最详细的第一个项目)
一.前言 (一).概述 Python下有许多款不同的 Web 框架.Django是重量级选手中最有代表性的一位.许多成功的网站和APP都基于Django. Django是一个开放源代码的Web应用框架 ...
- SPSS数据分析方法不知道如何选择
一提到数学,高等数学,线性代数,概率论与数理统计,数值分析,空间解析几何这些数学课程,头疼呀.作为文科生,遇见这些课程时,通常都是各种寻求帮助,班上有位宅男数学很厉害,各种被女生‘围观’,这数学为 ...