组合数据类型练习,英文词频统计实例上

1.字典实例:建立学生学号成绩字典,做增删改查遍历操作。

>>> d={'01':95,'02':92,'03':86,'04':70}
>>> print(d)
{'01': 95, '02': 92, '03': 86, '04': 70}
>>> d['05']=80
>>> print(d)
{'01': 95, '02': 92, '03': 86, '04': 70, '05': 80}
>>> d.pop('02')
92
>>> print(d)
{'01': 95, '03': 86, '04': 70, '05': 80}
>>> d['03']=88
>>> print(d)
{'01': 95, '03': 88, '04': 70, '05': 80}
>>> d['05']
80
>>>

2.列表,元组,字典,集合的遍历。
总结列表,元组,字典,集合的联系与区别。

>>> a=list('1234')
>>> b=tuple('1232')
>>> d={'01':95,'02':92,'03':86,'04':70}
>>> t=set('124')
>>> for i in a:
            print(i)


1
2
3
4
>>> for i in b:
            print(i)


1
2
3
2
>>> for i in d:
            print(i,d[i])


01 95
02 92
03 86
04 70
>>> for i in t:
            rint(i)


2
4
1
>>>

1)列表是任意对象的序列。列表用方括号表示。
(2)将一组值打包到一个对象中,称为元组。元组用圆括号表示。元组和列表的大部分操作相同。列表是不固定的,可以随时插入,删除

(3)与列表和元组不同,集合是无序的,也不能通过索引进行访问。此外,集合中的元素不能重复。
(4)字典就是一个关联数组或散列表,其中包含通过关键字索引的对象。用大括号表示。

 

3.英文词频统计实例

  1. A.待分析字符串
  2. B.分解提取单词
  3.    a.大小写 txt.lower()
       b.分隔符'.,:;?!-_’
       c.单词列表
    C.单词计数字典

s='''It's been a long day without you my friend

And I'll tell you all about it when I see you again

We've come a long way from where we began
Oh I'll tell you all about it when I see you again
When I see you again

Damn who knew all the planes we flew
Good things we've been through
That I'll be standing right here
Talking to you about another path
I know we loved to hit the road and laugh
But something told me that it wouldn't last
Had to switch up look at things different see the bigger picture
Those were the days hard work forever pays
Now I see you in a better place

How could we not talk about family when family's all that we got?
Everything I went through you were standing there by my side
And now you gonna be with me for the last ride
It's been a long day without you my friend

And I'll tell you all about it when I see you again

We've come a long way from where we began
Oh I'll tell you all about it when I see you again

When I see you again

First you both go out your way
And the vibe is feeling strong and what's
Small turn to a friendship a friendship
Turn into a bond and that bond will never
Be broke and the love will never get lost

And when brotherhood come first then the line
Will never be crossed established it on our own
When that line had to be drawn and that line is what
We reach so remember me when I'm gone

How could we not talk about family when family's all that we got?
Everything I went through you were standing there by my side
And now you gonna be with me for the last ride
Let the light guide your way hold every memory
As you go and every road you take will always lead you home

Hoo

It's been a long day without you my friend

And I'll tell you all about it when I see you again

We've come a long way from where we began
Oh I'll tell you all about it when I see you again
When I see you again
Again

When I see you again see you again

When I see you again'''
>>> s=s.lower()
>>> s=s.replace(',',' ')
>>> s=s.replace('\n',' ')
>>> s=s.split(' ')
>>> s.sort()
>>> di={}
>>> bing=set(s)
>>> for i in bing:
di[i]=s.count(i)
print(i,di[i])


16
into 1
line 3
what 1
way 5
established 1
memory 1
how 2
good 1
it 8
own 1
different 1
there 2
we've 4
forever 1
with 2
bond 2
home 1
were 3
vibe 1
i'm 1
it's 3
and 13
always 1
take 1
up 1
everything 2
you 31
as 1
days 1
something 1
could 2
family 2
feeling 1
but 1
flew 1
now 3
long 6
to 5
light 1
laugh 1
lead 1
never 3
go 2
told 1
where 3
love 1
small 1
loved 1
hoo 1
then 1
by 2
gonna 2
see 14
standing 3
right 1
on 1
all 9
reach 1
strong 1
planes 1
lost 1
last 3
will 4
i 15
our 1
bigger 1
i'll 7
better 1
wouldn't 1
picture 1
hard 1
began 3
not 2
for 2
place 1
me 4
through 3
who 1
is 2
knew 1
friend 3
things 2
the 10
both 1
guide 1
path 1
tell 6
be 6
here 1
turn 2
your 2
gone 1
get 1
out 1
without 3
every 2
ride 2
what's 1
pays 1
day 3
remember 1
we 10
broke 1
my 5
so 1
oh 3
friendship 2
in 1
had 2
been 4
got? 2
let 1
road 2
know 1
come 4
crossed 1
from 3
damn 1
that 7
hit 1
side 2
went 2
hold 1
brotherhood 1
talking 1
drawn 1
again 13
switch 1
work 1
at 1
a 10
about 9
look 1
when 16
another 1
first 2
family's 2
talk 2
those 1
>>>

posted @ 2017-09-22 15:47  007王俊祺  阅读(152)  评论(0编辑  收藏  举报