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

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

 

posted on 2017-09-22 16:23  201506050009曹艺健  阅读(130)  评论(0编辑  收藏  举报