组合数据类型,英文词词频统计

一、总结列表,元组,字典,集合的联系与区别:

区别:列表list,元组tuple是有顺序的,而字典dict和集合是没顺序的。列表是以[ ]形式表示,元组是以( )表示,字典以{ }表示,集合则是以[()]的形式表示。列表是可变对象,可以有增删改操作,而元组是只读的,不能修改。字典使用键-值(key-value)存储,键是不可变的对象。插入和查找效率高,不会随着键的增加而变慢,但是需要占用大量的内存。字典是用空间换取时间的一种方法。集合是一个无序不重复元素集, 基本功能包括关系测试和消除重复元素。

联系:列表,元组,字典,集合可以相互转换。

列表,元组,字典,集合的遍历:

列表是可变序列,元组是不可变序列;列表的值可以修改,而元祖的值初始化后不可修改,两者都是有序的。字典和集合 两者都是无序的,数据量大时可用集合或列表来创建
li= list(‘列表的遍历‘)      #列表的遍历
print(li)
for i in li:
    print(i)

tu = tuple(‘我爱上数据挖掘课‘)       #元组的遍历
print(tu)
for a in tu:
    print(a)

st = set(‘集合的遍历‘)        #集合的遍历
print(st)
for b in st:
    print(b)

dt = {‘a‘:80,‘b‘:79,‘c‘:90}        #字典的遍历
print(dt)
for i in dt:
    print(i,dt[i])

运行结果:

二、英文词频统计:

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

2、分隔出一个一个的单词 list

3、统计每个单词出现的次数 dict

代码:

#下载一首英文的歌词
str=‘‘‘There‘s a fire starting in my heart,Reaching a fever pitch and it‘s bringing me out the dark

Finally, I can see you crystal clear.

Go ahead and sell me out and I‘ll lay your shipbare.

See how I leave, with every piece of you

Don‘t underestimate the things that I will do.

There‘s a fire starting in my heart,

Reaching a fever pitch and it‘s bringing me out the dark

The scars of your love, remind me of us.

They keep me thinking that we almost had it all

The scars of your love, they leave me breathless

I can‘t help feeling...

We could have had it all

Rolling in the Deep

You had my heart... Inside of your hands

And you played it... To the beat

Baby I have no story to be told,

But I‘ve heard one of you and I‘m gonna make your head burn.

Think of me in the depths of your despair.

Making a home down there,as mine sure won‘t be shared.

The scars of your love,remind me of us.

They keep me thinking that we almost had it all

The scars of your love, they leave me breathless

I can‘t help feeling

"We could have had it all... Rolling in the Deep

You had my heart inside of your hand

And you played it To the beat

We could have had it all

Rolling in the deep.

You had my heart inside of your hand,

But you played it with your beat

Throw yourself through every open door (Whoa)

Count your blessings to find what look for (Whoa-uh)

Turn my sorrow into treasured gold (Whoa)

And pay me back in kind- You reap just what you‘ve sown.

"(You‘re gonna wish you... Never had met me)

We could have had it all (Tears are gonna fall... Rolling in the deep)

We could have had it all yeah ( you‘re gonna wish you... never had met me)

It all. (Tears are gonna fall)

It all

It all (Rolling in the deep)

We could have had it all (you‘re gonna wish you, never had met me)

Rolling in the deep (Tears are gonna fall rolling in the deep)

You had my heart inside... (you‘re gonna wish you)... of your hand (Never had met me)

And you played it... (Tears are gonna fall)... to the beat (Rolling in the deep)

We could have had it all ( you‘re wish you never had met me)

Rolling in the deep (tears are gonna fall, rolling in the deep)

You had my heart... ( you‘re gonna wish you)... Inside of your hand (Never had met me)"

But you played it

You played it.

You played it to the beat‘‘‘
#输出文章
print(str)
#f分隔出一个一个的单词
li1=str.split()
print(len(li1),li1)
#统计每个单词出现的次数
strset=set(li1) #用set把字符串转换成集合
for word in strset:
    print(word,li1.count(word))   #统计每一个单词在歌曲中的次数

运行结果:

.

.

.

原文地址:https://www.cnblogs.com/123-feng/p/9690841.html

时间: 2024-08-02 03:04:06

组合数据类型,英文词词频统计的相关文章

英文文件词频统计

import refrom collections import Countertxt = open('readme.txt',mode='r').read()#读取文件list1 = re.split('\W+',txt)#以不是英文字母来区分单词out1 = Counter(list1)#统计词频print('词频统计结果:',out1)print('出现频率最高的前十个单词:',out1.most_common(10)) 输出: 原文地址:https://www.cnblogs.com/c

英文小说词频统计

strYoung ='''young for you Gala sunday's coming i wanna drive my car to your apartment with present like a star forecaster said the weather may be rainy hard but i know the sun will shine for us oh lazy seagull fly me from the dark i dress my jeans a

201671010441徐浩杰 词频统计软件项目报告

实验二.软件工程个人项目 一.需求分析 尝试按照<构建之法>第2章中2.3所述PSP流程,使用JAVA编程语言,独立完成一个英文文本词频统计的软件开发.软件基本功能要求如下: •程序可读入任意英文文本文件,该文件中英文词数大于等于1个. •程序需要很壮健,能读取容纳英文原版<哈利波特>10万词以上的文章. •指定单词词频统计功能:用户可输入从该文本中想要查找词频的一个或任意多个英文单词,运行程序的统计功能可显示对应单词在文本中出现的次数和柱状图. •高频词统计功能:用户从键盘输入高

201671010431+词频统计软件项目报告

一.需求分析 按照<构建之法>第2章中2.3所述PSP流程,使用JAVA编程语言,独立完成一个英文文本词频统计的软件开发.软件基本功能要求如下: 1.程序可读入任意英文文本文件,该文件中英文词数大于等于1个. 2.程序需要很壮健,能读取容纳英文原版<哈利波特>10万词以上的文章. 3.指定单词词频统计功能:用户可输入从该文本中想要查找词频的一个或任意多个英文单词,运行程序的统计功能可显示对应单词在文本中出现的次数和柱状图. 4.高频词统计功能:用户从键盘输入高频词输出的个数k,运行

201671010416 焦少梅 实验二 词频统计项目

实验二 软件工程个人项目 实验目的与要求 掌握软件项目开发流程 掌握Github上发布软件项目的操作方法. 实验内容和步骤 任务1: 需求分析: 尝试按照<构建之法>第2章中2.3所述PSP流程,使用JAVA编程语言,独立完成一个英文文本词频统计的软件开发 程序可读入任意英文文本文件,该文件中英文词数大于等于1个. 程序需要很壮健,能读取容纳英文原版<哈利波特>10万词以上的文章. 指定单词词频统计功能:用户可输入从该文本中想要查找词频的一个或任意多个英文单词运行程序的统计功能可显

201671030119 词频统计软件项目报告

项目名称:词频统计软件 源码 需求分析 - 使用JAVA编程语言,独立完成一个英文文本词频统计的软件开发 - 软件基本功能要求如下: 1.程序可读入任意英文文本文件,该文件中英文词数大于等于1个. 2.程序需要很壮健,能读取容纳英文原版<哈利波特>10万词以上的文章. 3.指定单词词频统计功能:用户可输入从该文本中想要查找词频的一个或任意多个英文单词,运行程序的统计功能可显示对应单词在文本中出现的次数和柱状图. 4.高频词统计功能:用户从键盘输入高频词输出的个数k,运行程序统计功能,可按文本中

201671010402 词频统计软件项目报告

需求分析 可以使用Java编程语言,独立完成一个英文文本词频统计的软件开发. 程序可读入任意英文文本文件,该文件中英文词数大于等于1个. 程序需要很壮健,能读取容纳英文原版<哈利波特>10万词以上的文章. 测试环境 此次项目的测试机为Windows环境. 使用的JDK版本为 jdk8u161. 使用的JRE版本为jre8u161. 基本功能需求 用户可输入从该文本中想要查找词频的一个或任意多个英文单词,运行程序的统计功能可显示对应单词在文本中出现的次数. 统计该文本所有单词数量及词频数,并能将

c++实现词频统计

需求:从一个英文txt中读取内容,实现词频统计. 现完成:基本功能大概完成了,由于编程基础比较差,文件操作部分还不是很熟练,我发现从文件中提取字符串流读取到程序的string对象中,会把所有的空格过滤掉,导致没法统计单词频率,目前还没找到解决方法,只能先手动输入文章了.ORZ... 好好学习java,目前看来,处理字符串等问题还是java有成熟的解决方案. 1 /** 2 * 对一段英文的词频统计 3 4 */ 5 #include <iostream> 6 #include <stri

词频统计Web工程

本次将原本控制台工程迁移到了web工程上.. 需求: 1.把程序迁移到web平台,通过用户上传TXT的方式接收文件: 2.在页面上给出链接 (如果有封皮.作者.字数.页数等信息更佳)或表格,展示经典英文小说词频统计结果: 3. 用柱状图进行数据展示. 4. 图表上只显示文件名.总次数.排序后的前30个单词. 5.不能识别"it's,I'm,he's"这样的连词. 实现: 1. 上传文档. 2. 统计所有单词数目. 3. 统计文档的总行数. 4. 统计文档中相邻两个单词出现的次数. 5.