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

1.列表实例:由字符串创建一个作业评分列表,做增删改查询统计遍历操作。例如,查询第一个3分的下标,统计1分的同学有多少个,3分的同学有多少个等。


score=list('3212233331133213') print('作业评分列表:',score) score.append('1') print('增加:',score) score.pop() print('删除:',score) score.insert(2,'3') print('插入:',score) score[2]='3' print('修改:',score) print('第一个3分的下标:',score.index('3')) print('1分的个数:',score.count('1')) print('3分的个数:',score.count('3'))

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

s={'小雨':'1','小黑':'3','小白':'2'}
print('查询小雨的值:',s.get('小雨'))
s.pop('小白')
s['小李']='5'
print('移除小白和增加小李:',s)
s['小李']='6'
print('修改小李:',s)

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

a=list('123456789')
b={'小雨':'1','小白':'3','小黑':'2'}
c=(1, 2, 3, 4, 5 )
print('列表的遍历为:',end='')
for i in a:
    print(i,end='')
print()
print('元祖的遍历为:',end='')
for i in c:
    print(i,end='')
print()
print('字典key的遍历为:',end='')
for i in b:
    print(i,end='')
print()
print('字典value的遍历为:',end='')
for i in b.values():
    print(i,end='')
print()
print('数组的遍历为:',end='')
for i in c:
    print(i,end='')

 

 

4.英文词频统计实例

s='''Recently, more and more students would like to go to net bars,
especially students. It has become quite commom to go there. They
waste too much precious time on playing video games in the net bar.
As a result, not only does it do harm to their health but also produces
a bad effect on their studies. Their health becomes worse and worse
and their studies are neglected. Now a lot of net bars have been shut
down in Beijing.But some students go to the net bars at suburbs to play.'''
print("do出现次数",s.count('do'))
s=s.replace(",","")
s=s.replace('.','')
s=s.replace('?','')
s=s.replace('!','')
s=s.replace('\n','')
s=s.lower()
s1=s.split(' ')
key=set(s1)
dic={}
for i in key:
    dic[i]=s.count(i)
wc=list(dic.items())
wc.sort(key=lambda x:x[1],reverse=True)
for i in range(10):
print(wc[i])

 

posted on 2017-09-21 16:01  09陈雨雨  阅读(141)  评论(0编辑  收藏  举报