码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2017-09-26 19:27:45      阅读:235      评论:0      收藏:0      [点我收藏+]

标签:home   code   txt   区别   white   count   pre   练习   hit   

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

>>> l={01:79,"02":88,"03":89,"04":74,"05":46,"06":86,07:90,08:98}
>>> l
{01: 79, 02: 88, 03: 89, 04: 74, 05: 46, 06: 86, 07: 90, 08: 98}
>>> l[05]
46
>>> l[09]=95
>>> l
{01: 79, 02: 88, 03: 89, 04: 74, 05: 46, 06: 86, 07: 90, 08: 98, 09: 95}
>>> l.pop(07)
90
>>> l
{01: 79, 02: 88, 03: 89, 04: 74, 05: 46, 06: 86, 08: 98, 09: 95}
>>> l[04]=88
>>> l
{01: 79, 02: 88, 03: 89, 04: 88, 05: 46, 06: 86, 08: 98, 09: 95}
>>> 

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

>>> list=list(8656853529)
>>> list
[8, 6, 5, 6, 8, 5, 3, 5, 2, 9]
>>> tuple=tuple(136145314148)
>>> tuple
(1, 3, 6, 1, 4, 5, 3, 1, 4, 1, 4, 8)
>>> set=set(list)
>>> set
{3, 5, 8, 9, 6, 2}
>>> for i in list:
    print(i)

    
8
6
5
6
8
5
3
5
2
9
>>> for i in tuple:
    print(i)

    
1
3
6
1
4
5
3
1
4
1
4
8
>>> for i in set:
    print(i)

    
3
5
8
9
6
2
>>> dic=dict(zip(list,tuple))
>>> dic
{8: 4, 6: 1, 5: 1, 3: 3, 2: 4, 9: 1}
>>> for i in dic:
    print(i,dic[i])

    
8 4
6 1
5 1
3 3
2 4
9 1
>>> 

3、英文词频统计实例

  1、待分析字符串

k=‘‘‘May you bloom and grow! 
Bloom and grow forever 
Edelweiss, edelweiss 
Bless my homeland forever ?
Small and white 
Clean and bright 
You look happy to meet me 
Blossom of snow 
May you bloom and grow 
Bloom and grow forever 
Edelweiss, edelweiss 
Bless my homeland forever?
Edelweiss, edelweiss,
Every morning you greet me.
Small and white,
Clean and bright,
You look happy to meet me.
Blossom of snow,
May you bloom and grow,
Bloom and grow forever.
Edelweiss, edelweiss,
Bless my homeland forever.
Small and white,
Clean and bright,
You look happy to meet me.
Blossom of snow
May you bloom and grow,
Bloom and grow forever.
Edelweiss, edelweiss,
Bless my homeland forever.
‘‘‘

 

  2、分解提取单词

       1、大小写 txt.lower()

k=k.lower()
print(k)
may you bloom and grow! 
bloom and grow forever 
edelweiss, edelweiss 
bless my homeland forever ?
small and white 
clean and bright 
you look happy to meet me 
blossom of snow 
may you bloom and grow 
bloom and grow forever 
edelweiss, edelweiss 
bless my homeland forever?
edelweiss, edelweiss,
every morning you greet me.
small and white,
clean and bright,
you look happy to meet me.
blossom of snow,
may you bloom and grow,
bloom and grow forever.
edelweiss, edelweiss,
bless my homeland forever.
small and white,
clean and bright,
you look happy to meet me.
blossom of snow
may you bloom and grow,
bloom and grow forever.
edelweiss, edelweiss,
bless my homeland forever.

 

       2、分隔符‘.,:;?!-_’

k=k.lower()
for i in,.?!:
    k=k.replace(i, )
print(k)
may you bloom and grow  
bloom and grow forever 
edelweiss  edelweiss 
bless my homeland forever  
small and white 
clean and bright 
you look happy to meet me 
blossom of snow 
may you bloom and grow 
bloom and grow forever 
edelweiss  edelweiss 
bless my homeland forever 
edelweiss  edelweiss 
every morning you greet me 
small and white 
clean and bright 
you look happy to meet me 
blossom of snow 
may you bloom and grow 
bloom and grow forever 
edelweiss  edelweiss 
bless my homeland forever 
small and white 
clean and bright 
you look happy to meet me 
blossom of snow
may you bloom and grow 
bloom and grow forever 
edelweiss  edelweiss 
bless my homeland forever 
 w=k
    w=w.split( )
    
print(w)
[may, you, bloom, and, grow, ‘‘, \nbloom, and, grow, forever, \nedelweiss, ‘‘, edelweiss, \nbless, my, homeland, forever, ‘‘, \nsmall, and, white, \nclean, and, bright, \nyou, look, happy, to, meet, me, \nblossom, of, snow, \nmay, you, bloom, and, grow, \nbloom, and, grow, forever, \nedelweiss, ‘‘, edelweiss, \nbless, my, homeland, forever, \nedelweiss, ‘‘, edelweiss, \nevery, morning, you, greet, me, \nsmall, and, white, \nclean, and, bright, \nyou, look, happy, to, meet, me, \nblossom, of, snow, \nmay, you, bloom, and, grow, \nbloom, and, grow, forever, \nedelweiss, ‘‘, edelweiss, \nbless, my, homeland, forever, \nsmall, and, white, \nclean, and, bright, \nyou, look, happy, to, meet, me, \nblossom, of, snow\nmay, you, bloom, and, grow, \nbloom, and, grow, forever, \nedelweiss, ‘‘, edelweiss, \nbless, my, homeland, forever, \n]

 

       3、单词列表

[may, you, bloom, and, grow, ‘‘, \nbloom, and, grow, forever, \nedelweiss, ‘‘, edelweiss, \nbless, my, homeland, forever, ‘‘, \nsmall, and, white, \nclean, and, bright, \nyou, look, happy, to, meet, me, \nblossom, of, snow, \nmay, you, bloom, and, grow, \nbloom, and, grow, forever, \nedelweiss, ‘‘, edelweiss, \nbless, my, homeland, forever, \nedelweiss, ‘‘, edelweiss, \nevery, morning, you, greet, me, \nsmall, and, white, \nclean, and, bright, \nyou, look, happy, to, meet, me, \nblossom, of, snow, \nmay, you, bloom, and, grow, \nbloom, and, grow, forever, \nedelweiss, ‘‘, edelweiss, \nbless, my, homeland, forever, \nsmall, and, white, \nclean, and, bright, \nyou, look, happy, to, meet, me, \nblossom, of, snow\nmay, you, bloom, and, grow, \nbloom, and, grow, forever, \nedelweiss, ‘‘, edelweiss, \nbless, my, homeland, forever, \n]

 

 ’3、单词计数字典

k=k.lower()
for i in,.?!:
    k=k.replace(i, )
    w=k
    w=w.split( )   
print(w)
dict={}
for i in w:
    dict[i]=w.count(i)
    print(i,dict[i])
may 1
you 5
bloom 4
and 14
grow 8
 7

bloom 4
and 14
grow 8
forever 8

edelweiss 5
 7
edelweiss 5

bless 4
my 4
homeland 4
forever 8
 7

small 3
and 14
white 3

clean 3
and 14
bright 3

you 3
look 3
happy 3
to 3
meet 3
me 4

blossom 3
of 3
snow 2

may 2
you 5
bloom 4
and 14
grow 8

bloom 4
and 14
grow 8
forever 8

edelweiss 5
 7
edelweiss 5

bless 4
my 4
homeland 4
forever 8

edelweiss 5
 7
edelweiss 5

every 1
morning 1
you 5
greet 1
me 4

small 3
and 14
white 3

clean 3
and 14
bright 3

you 3
look 3
happy 3
to 3
meet 3
me 4

blossom 3
of 3
snow 2

may 2
you 5
bloom 4
and 14
grow 8

bloom 4
and 14
grow 8
forever 8

edelweiss 5
 7
edelweiss 5

bless 4
my 4
homeland 4
forever 8

small 3
and 14
white 3

clean 3
and 14
bright 3

you 3
look 3
happy 3
to 3
meet 3
me 4

blossom 3
of 3
snow
may 1
you 5
bloom 4
and 14
grow 8

bloom 4
and 14
grow 8
forever 8

edelweiss 5
 7
edelweiss 5

bless 4
my 4
homeland 4
forever 8

 1
>>> 

 

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

标签:home   code   txt   区别   white   count   pre   练习   hit   

原文地址:http://www.cnblogs.com/095lqt/p/7598047.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!