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

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

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

 

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

dir={'大':32,'二':55,'强':23,'光':76}
print('学生成绩字典:',dir)
dir['大']=87
print('增加后的成绩字典:',dir)
d.pop('强')
print('删除后的成绩字典:',dir)
dir['二']=32
print('修改后的成绩字典:',dir)
print('查询二成绩:',dir.get('二'))

 

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

l=list('143216543246531')
y=set('3547813254651')
dir={'大':'423','二':'223','强':'412'}
x=(1,2,3,4,5)
print('列表的遍历为:',end='')
for i in l:
print(i,end='')print('元组的遍历为:',end='')
for i in y:
print(i,end='')print('字典key的遍历为:',end='')
for i in dir:
print(i,end='')print('字典value的遍历为:',end='')
for i in c.values():
print(i,end='')print('集合的遍历为:',end='')
for i in x:
print(i,end='')

 

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

4.英文词频统计实例

    A.待分析字符串

    B.分解提取单词

        a.大小写 txt.lower()

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

    C.计数字典

 

    D.排序list.sort()

  •     E.输出TOP(10)
  • s='''Well I wonder could it be When I was dreaming about you baby
    You were dreaming of me Call me crazy Call me blind To still be suffering
    is stupid after all of this time Did I lose my love to someone better
    And does she love you like I do I do, you know I really really do
    Well hey So much I need to say Been lonely since the day The day you went away
    So sad but true For me there‘s only you Been crying since the day
    the day you went away.!?'''
    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 @ 2017-09-21 20:23  sister姐姐  阅读(110)  评论(0编辑  收藏  举报