组合数据类型练习,英文词频统计实例上
Posted 018林彬
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了组合数据类型练习,英文词频统计实例上相关的知识,希望对你有一定的参考价值。
1、字典实例:建立学生学号成绩字典,做增删改查遍历操作。
建立:
d={‘0001‘:‘99‘,‘0003‘:‘89‘,‘0004‘:‘98‘,‘0005‘:‘100‘,‘0006‘:‘78‘}
增:
d[‘0002‘]=‘79‘
删:
d.pop(‘0001‘)
改:
d[‘0004‘]=100
查:
print(d[‘0002‘])
遍历操作:
for i in d:
print(d)
2、列表,元组,字典,集合的遍历。
l=list(‘12438849374837‘)
for i in l:
print(l)
t=tuple(‘nhaildhabvka‘)
for i in t:
print(t)
d={‘0001‘:‘99‘,‘0003‘:‘89‘,‘0004‘:‘98‘,‘0005‘:‘100‘,‘0006‘:‘78‘}
for i in d:
print(d)
s=set(l)
for i in s:
print(s)
总结列表,元组,字典,集合的联系与区别。
列表:是一组有序序列,能进行查询,增加,删除,修改。是用中括号【】括起来的。
元组:是一组有序序列,能进行查询。是用小括号()括起来的。
字典:字典不是有序序列,是由一组组键值对组成。能进行增删改查等操作。是用大括号{}括起来的。
集合:集合也不是有序序列,能进行查询等操作。是用大括号{}括起来的。
3、英文词频统计实例
(1)待分析字符串
bin=‘‘‘Put your make up on
Get your nails done
Curl your hair
Run the extra mile
Keep it slim
so they like you, do they like you?
Get your sexy on
Don‘t be shy, girl
Take it off
This is what you want, to belong, so they like you
Do you like you?
You don‘t have to try so hard
You don‘t have to, give it all away
You just have to get up, get up, get up, get up
You don‘t have to change a single thing
You don‘t have to try, try, try, try
You don‘t have to try, try, try, try
You don‘t have to try, try, try, try
You don‘t have to try
Yooou don‘t have to try
Oooh Oooh
Get your shopping on, at the mall, max your credit card
You don‘t have to choose, buy it all, so they like you
Do they like you?
Wait a second,Why, should you care, what they think of you
When you‘re all alone, by yourself, do you like you?
Do you like you?You don‘t have to try so hard
You don‘t have to, give it all away
You just have to get up, get up, get up, get up
You don‘t have to change a single thing
You don‘t have to try so hard
You don‘t have to bend until you break
You just have to get up, get up, get up, get up
You don‘t have to change a single thing
You don‘t have to try, try, try, try
You don‘t have to try, try, try, try
You don‘t have to try
You don‘t have to try
You don‘t have to try, try, try, try
You don‘t have to try, try, try, try
You don‘t have to try, try, try, try
You don‘t have to tryYooou don‘t have to try
NooooOooh
You don‘t have to try so hard
You don‘t have to, give it all away
You just have to get up, get up, get up, get up
You don‘t have to change a single thing
You don‘t have to try, try, try, tryYou don‘t have to try, try, try, try
You don‘t have to try, try, try, try
You don‘t have to try
Take your make up off
Put your head down
Take a breath
Look into the mirror, at yourself.
Don‘t you like you?
Cause I like you!‘‘‘
(2)分解提取单词
大小写 txt.lower()
bin=bin.lower()
分隔符‘.,:;?!-_’
for i in ‘,.?!\n‘:
bin=bin.replace(i,‘ ‘)
单词列表
bin=bin.split(" ")
(3)单词计数字典
words=set(bin)
d={}
d[‘star‘]=bin.count(‘star‘)
for a in words:
d[a]=bin.count(a)
for a in d:
print("{0:<11}{1}".format(a,d[a]))
以上是关于组合数据类型练习,英文词频统计实例上的主要内容,如果未能解决你的问题,请参考以下文章