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

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

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

 

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

dir={'熊大':23,'熊二':45,'光头强':66,'光头强老板':78}
print('学生成绩字典:',dir)
dir['熊老大']=88
print('增加后的成绩字典:',dir)
d.pop('光头强')
print('删除后的成绩字典:',dir)
dir['熊二']=44
print('修改后的成绩字典:',dir)
print('查询熊二成绩:',dir.get('熊二'))

 

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

l=list('143216543246531')
y=set('3547813254651')
dir={'熊大':'34','熊二':'53','光头强':'67'}
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:19  丁镜钿  阅读(140)  评论(0编辑  收藏  举报