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

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

时间:2017-09-26 14:55:09      阅读:158      评论:0      收藏:0      [点我收藏+]

标签:字符串   组合   replace   类型   组合数   reverse   set   key   长度限制   

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

names=["佐助","鸣人","小樱","雏田"]
scores=["98","60","80","70"]
c=dict(zip(names,scores))
#
c[鹿丸]="100"
#
c[佐助]="99"
#
c.pop(鹿丸)

技术分享

2.列表,元组,字典,集合的遍历。

#集合遍历:
s=set("564132564")
for i in s:
    print(i)

#元组遍历:
a=1,2,3,4,5
for i in a:
    print(i)

#列表遍历:
n=list("3243242")
for i in n:
    print(i)

#字典遍历:
names=["佐助","鸣人"]
scores=["98","60"]
b=dict(zip(names,scores))
for i in b:
    print(i,b[i])

技术分享


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

列表:是一种有序的序列,可以随时添加和删除其中的元素,没有长度限制、元素类型可以不同。

元组:和list非常相似,但是一旦初始化便不能修改。

字典:使用键-值进行存储,其中键必须为不可变的对象。

集合:值不能重复,并且是无序的。

 

3.英文词频统计实例

待分析字符串

分解提取单词

大小写 txt.lower()

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

单词列表

单词计数字典

str=‘‘‘I‘ve been reading books of old
The legends and the myths
Achilles and his gold
Hercules and his gifts
Spiderman‘s control
And Batman with his fists
And clearly I don‘t see myself upon that list
She said, where‘d you wanna go?
How much you wanna risk?
I‘m not looking for somebody
With some superhuman gifts
Some superhero
Some fairytale bliss
Just something I can turn to
Somebody I can kiss
I want something just like this
Doo-doo-doo, doo-doo-doo
Doo-doo-doo, doo-doo
Doo-doo-doo, doo-doo-doo
Oh I want something just like this
Doo-doo-doo, doo-doo-doo
Doo-doo-doo, doo-doo
Doo-doo-doo, doo-doo-doo
Oh I want something just like this
I want something just like this
I‘ve been reading books of old
The legends and the myths
The testaments they told
The moon and its eclipse
And Superman unrolls
A suit before he lifts
But I‘m not the kind of person that it fits
She said, where‘d you wanna go?
How much you wanna risk?
I‘m not looking for somebody
With some superhuman gifts
Some superhero
Some fairytale bliss
Just something I can turn to
Somebody I can miss
I want something just like this
I want something just like this
Oh I want something just like this
Doo-doo-doo, doo-doo-doo
Doo-doo-doo, doo-doo
Doo-doo-doo, doo-doo-doo
Oh I want something just like this
Doo-doo-doo, doo-doo-doo
Doo-doo-doo, doo-doo
Doo-doo-doo, doo-doo-doo
Where‘d ya wanna go?
How much you wanna risk?
I‘m not looking for somebody
With some superhuman gifts
Some superhero
Some fairytale bliss
Just something I can turn to
Somebody I can kiss
I want something just like this
Oh I want something just like this
Oh I want something just like this
Oh I want something just like this‘‘‘

#将所有大写转换为小写
str=str.lower()
print(全部转换为小写的结果:+str+\n)

#将所有将所有其他做分隔符(,.?!)替换为空格
for i in ,.?!::
    str=str.replace(i, )
print(其他分隔符替换为空格的结果:+str+\n)

#分隔出一个一个单词
str=str.split( )
print(分隔结果为:,str,\n)

word = set(str)
dic={}
for i in word:
    dic[i]= str.count(i)
    
str=list(dic.items())
str.sort(key=lambda x:x[1],reverse=True)
print(str,\n)
print(词频前10为:)
for i in range(10):
    word,count=str[i]
    print({}\t{}.format(word,count))

技术分享

技术分享

 

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

标签:字符串   组合   replace   类型   组合数   reverse   set   key   长度限制   

原文地址:http://www.cnblogs.com/ffr0068/p/7596438.html

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