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

本文通过Python实现字典的基本操作,并演示了如何利用列表、元组、字典和集合进行遍历操作。此外,还提供了一个详细的英文文本词频统计实例,包括文本预处理、单词提取和词频统计。

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))

 

posted on 2017-09-26 13:58 068冯斐然 阅读( ...) 评论( ...) 编辑 收藏

转载于:https://www.cnblogs.com/ffr0068/p/7596438.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值