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

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

时间:2017-09-22 16:34:09      阅读:137      评论:0      收藏:0      [点我收藏+]

标签:echo   family   ica   gif   zed   man   分隔符   replace   递增   

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

d={‘姓名‘:‘蛋蛋‘,‘学号‘:‘20150605‘,‘成绩‘:‘55‘}
print(d)
print(‘姓名:‘,d[‘姓名‘])
print(‘key:‘,d.keys())
print(‘values:‘,d.values())
d[‘性别‘]=‘男‘              
print(d)
d[‘性别‘]=‘人妖‘           
print(d)
d.pop(‘性别‘)      #del d[‘性别‘]#
print(d)
for i in d:
    print(‘遍历:‘,i,d[i])
技术分享

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

ls=list(‘123452356‘)
tu=tuple(‘123456789‘)
d={‘1‘:‘1‘,‘3‘:‘3‘}
s=set(ls)
print(ls)
print(tu)
print(s)
print(d)

for i in ls:
    print(‘ls‘,i)

for i in tu:
    print(‘tu‘,i)

for i in s:
    print(‘s‘,i)

for i in d:
    print(‘d:‘,i,d[i])
技术分享

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

1)list是一种有序的序列,正向递增、反向递减序号。可以随时添加和删除其中的元素,没 有长度限制,元素类型可以不同。

2)tuple和list非常类似,都是有序的序列。但是tuple一旦初始化就不能修改。

3)dict使用键-值(key-value)存储,key是不可变的对象。插入和查找速度快,不会随着key的增加而变慢,需要占用大量的内存。dict是用空间换取时间的一种方法。

 

3英文词频统计实例

待分析字符串

分解提取单词

大小写 txt.lower()

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

单词列表

单词计数字典

 d=‘‘‘Buddy the parrot ordered a ?10 ($13.50) set of gift boxes via Amazon’s Alexa voice-controlled system, The Sun reported.

The mystery order triggered an inquest in Corienne Pretorius’s house in southeast London, but after ruling out her family, she figured out Buddy was to blame after hearing him interact with the speaker.

"I couldn’t believe it when I realized that Buddy had made an Amazon order," the South African said.

Users can shout commands to the Amazon Echo speaker to access a host of services. It responds to the name Alexa.‘‘‘

dd=dd.lower()

for i in ‘,.?()’‘"‘:
    dd=dd.replace(i,‘ ‘)
words=dd.split(‘ ‘)  
dic={}
d=set(words)
for i in d:
    dic[i]=0
for i in words:
    dic[i]=dic[i]+1
print(dic)
for i in dic:
    print(i,dic[i])

技术分享

 

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

标签:echo   family   ica   gif   zed   man   分隔符   replace   递增   

原文地址:http://www.cnblogs.com/cyj5201314/p/7575517.html

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