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. 读取某个目录下的所有图片并显示到pictureBox

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...

  2. Redis的高可用(HA)

    本文参考 [https://www.jianshu.com/p/501c9c3b1b36] [https://www.jianshu.com/p/3b9054d3894b] 八大特性 1.速度快 正常 ...

  3. spark sql/hive小文件问题

    针对hive on mapreduce 1:我们可以通过一些配置项来使Hive在执行结束后对结果文件进行合并: 参数详细内容可参考官网:https://cwiki.apache.org/conflue ...

  4. 第一章 .NET基础-1.1.学前入门

    一.1.1. 概念:.NET和C# l .NET/DOTNET:一般指.Net Framework框架.一种平台,一种技术.它提供了一个稳定的运行环境:来保障我们.Net平台正常的运转. l C#(C ...

  5. centos7不能连接外网

    1.  首先保证虚拟机是NAT模式 2.  打开cmd窗口,输入ipconfig,查看vmnet8的ipv4地址是多少,DNS也需要记下,后面会用到 注意:vmnet8的ip要与虚拟机的网关IP在同一 ...

  6. 深入浅出TypeScript(2)- 用TypeScript创建web项目

    前言 在第一篇中,我们简单介绍了TypeScript的一些简单语法,那么如果我们只是简单使用TypeScript开发一个web项目,应该做哪些准备?接下来我们就结合TypeScript和Webpack ...

  7. CentOS -- Redis 3.2.12 Standalone Install and Configuration

    1 Tune OS setting  echo never > /sys/kernel/mm/transparent_hugepage/enabled echo "vm.overcom ...

  8. unity游戏开发_对象池

    现在假设游戏中我们需要实现一个这样功能,按下鼠标左键,发射一颗子弹,3秒之后消失.在这个功能中,我们发射了上百上千发子弹,就需要实例化生成上百上千次.这时候,我们就需要使用对象池这个概念,每次实例化生 ...

  9. 原生js之Math对象

    1.比较方法(常用) Math.min() //求一组数中的最小值 不能是数组,和对象等等. Math.max() //求一组数中的最大值eg:Math.min(5,3,5) // 3 2.取整(常用 ...

  10. CentOS 7 下 JDK1.8+Maven+Nginx+MySql+Git+Redis环境安装

    CentOS 7 下 JDK1.8+Maven+Nginx+MySql+Git+Redis环境安装 安装目录准备 新建data目录,用来放下载的软件 mkdir -p /data 切换到该data目录 ...