组合数据类型练习,英文词频统计实例上
Posted 钟嘉丽
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了组合数据类型练习,英文词频统计实例上相关的知识,希望对你有一定的参考价值。
- 字典实例:建立学生学号成绩字典,做增删改查遍历操作。
a={\'1\':90,\'2\':89,\'3\':88,\'4\':98}
print(\'字典\',a)
a[\'5\']=99
print(\'增加5学生成绩为99\',a)
a.pop(\'1\')
print(\'删除1的学生成绩\',a)
print(\'查找3的学生成绩\',a.get(\'3\'))
print(\'修改4的学生成绩为87\',a) -
- 列表,元组,字典,集合的遍历。 总结列表,元组,字典,集合的联系与区别。
#列表
l = list(\'23132111123132\')
#元组
t = tuple(\'1233211232123\')
#字典
d = dict(zip([1,2,3,4],[4,3,2,1]))
#集合
s = set(l)
for i in l:
print(i)
for i in t:
print(i)
for i in s:
print(i)
for i in d:
print(i) -
列表和元组是有序的,字典和集合都是无序的。
列表用“[]”表示。列表是可变对象,它支持在原处修改的操作.也可以通过指定的索引和分片获取元素。
元组用“()”表示。元组是只读的,不能修改。元组一旦定义其长度和内容都是固定的。
字典用“{}”表示。字典存储键值对(key-value)数据。每一组用冒号连起来,然后各组用逗号隔开。
集合没有特殊的表示方法,而是通过一个set函数转换成集合。集合内的元素没有重复的元素,主要是消除重复元素。
- 英文词频统计实例
- 待分析字符串
- 分解提取单词
- 大小写 txt.lower()
- 分隔符\'.,:;?!-_’
- 单词列表
- 单词计数字典
l=\'\'\'When I was dreaming about you baby
You were dreaming of me
Call me crazy
Call me blind
To still be suffering is stupid after all of this time
Did I lose my love to someone better
And does she love you like I do
I do, you know I really really do
Well hey
So much I need to say
Been lonely since the day
The day you went away
So sad but true
For me there\'s only you
Been crying since the day
the day you went away
I remember date and time
September twenty second
Sunday twenty five after nine
In the doorway with your case
No longer shouting at each other
There were tears on our faces
And we were letting go of something special
Something we\'ll never have again
I know, I guess I really really know
Why do we never know what we\'ve got till it\'s gone
How could I carry on the day you went away
Cause I\'ve been missing you so much I have to say
just one last dance\'\'\'
print(\'待分析字符串\',l)for i in \',.?!\\n\':
l=l.replace(i,\' \')
l=l.lower()
l=l.split(" ")
print(\'单词计数词典\')words=set(l)
dt={}
dt[\'day\']=l.count(\'day\')
dt[\'you\']=l.count(\'you\')
dt[\'so\']=l.count(\'so\')
dt[\'me\']=l.count(\'me\')
for j in words:
dt[j]=l.count(j)
for j in dt:
print("{0:<11}{1}".format(j,dt[j]))
print("{0:<11}{1}".format(j,dt[j]))运行结果:
================= RESTART: C:/Users/helloworld/Desktop/3.py =================
待分析字符串 When I was dreaming about you baby
You were dreaming of me
Call me crazy
Call me blind
To still be suffering is stupid after all of this time
Did I lose my love to someone better
And does she love you like I do
I do, you know I really really do
Well hey
So much I need to say
Been lonely since the day
The day you went away
So sad but true
For me there\'s only you
Been crying since the day
the day you went away
I remember date and time
September twenty second
Sunday twenty five after nine
In the doorway with your case
No longer shouting at each other
There were tears on our faces
And we were letting go of something special
Something we\'ll never have again
I know, I guess I really really know
Why do we never know what we\'ve got till it\'s gone
How could I carry on the day you went away
Cause I\'ve been missing you so much I have to say
just one last dance
单词计数词典
day 5
day 5
you 9
you 9
so 3
so 3
me 4
me 4
2
2
other 1
other 1
about 1
about 1
no 1
no 1
with 1
with 1
crying 1
crying 1
i 12
i 12
second 1
second 1
this 1
this 1
there 1
there 1
lose 1
lose 1
again 1
again 1
time 2
time 2
really 4
really 4
longer 1
longer 1
only 1
only 1
dreaming 2
dreaming 2
well 1
well 1
at 1
at 1
could 1
could 1
is 1
is 1
each 1
each 1
september 1
september 1
there\'s 1
there\'s 1
something 2
something 2
special 1
special 1
last 1
last 1
shouting 1
shouting 1
were 3
were 3
to 4
to 4
be 1
be 1
we\'ll 1
we\'ll 1
call 2
call 2
what 1
what 1
we\'ve 1
we\'ve 1
did 1
did 1
one 1
one 1
faces 1
faces 1
for 1
for 1
i\'ve 1
i\'ve 1
tears 1
tears 1
doorway 1
doorway 1
the 6
the 6
like 1
like 1
missing 1
missing 1
got 1
got 1
someone 1
someone 1
case 1
case 1
she 1
she 1
letting 1
letting 1
better 1
better 1
we 2
we 2
baby 1
baby 1
love 2
love 2
sunday 1
sunday 1
away 3
away 3
know 4
know 4
does 1
does 1
true 1
true 1
dance 1
dance 1
just 1
just 1
still 1
still 1
hey 1
hey 1
but 1
but 1
carry 1
carry 1
remember 1
remember 1
blind 1
blind 1
go 1
go 1
need 1
need 1
never 2
never 2
when 1
when 1
say 2
say 2
crazy 1
crazy 1
been 3
been 3
after 2
after 2
since 2
since 2
it\'s 1
it\'s 1
all 1
all 1
suffering 1
suffering 1
stupid 1
stupid 1
sad 1
sad 1
in 1
in 1
our 1
our 1
guess 1
guess 1
much 2
much 2
of 3
of 3
went 3
went 3
your 1
your 1
nine 1
nine 1
on 2
on 2
my 1
my 1
cause 1
cause 1
was 1
was 1
twenty 2
twenty 2
and 3
and 3
do 4
do 4
lonely 1
lonely 1
date 1
date 1
have 2
have 2
five 1
five 1
gone 1
gone 1
how 1
how 1
till 1
till 1
why 1
why 1
以上是关于组合数据类型练习,英文词频统计实例上的主要内容,如果未能解决你的问题,请参考以下文章