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

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

#建立学生学号成绩字典
d={'01':'80','02':'90','03':'89','04':'90','05':'100'}
print(d)

#增遍历操作
d['06']='100'

#删遍历操作
d.pop('01')

#改遍历操作
d['04']='100'

#查遍历操作
print(d['04'])

for i in d:
    print(d)

 

 

 

 

 

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

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

G=list('123345237646')
for i in G:
    print(G)
    
S=tuple('abcdefghi')
for i in S:
    print(S)

d={'01':'70','02':'90','03':'100','05':'79'}
for i in d:
    print(d)

se=set('12435433224')
for i in se:
    print(se)

 

 

 

 

 

 

3、英文词频统计实例

s='''You were the shadow to my light

Did you feel us?

Another star

You fade away

Afraid our aim is out of sight


Were you only imaginary?

Where are you now?

Atlantis

Where are you now?

Another dream

These shallow waters never met what I needed

I'm letting go a deeper dive

Eternal silence of the sea. I'm breathing alive

Where are you now?

Where are you now?

'''
s=s.lower()
print("全部大写转换为小写:"+s)

for i in ',!?':
    s=s.replace(i,' ') 
    print('替换结果:'+s)
    
s=s.split(' ')
print('分隔结果为:',s)

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

 

posted on 2017-09-26 19:35  欢喜小卢  阅读(82)  评论(0编辑  收藏  举报

导航