zoukankan      html  css  js  c++  java
  • 组合数据类型练习,英文词频统计实例

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

     1 work = list("1232122331231")
     2 print(work)
     3 
     4 work.append('3')
     5 print(work[-1])
     6 
     7 work.pop()
     8 print(work[-1])
     9 
    10 work[-1] = 2
    11 print(work[-1])
    12 
    13 print(work.index('3'))
    14 print(work.count('1'))
    15 print(work.count('3'))

    输出结果:

    ['1', '2', '3', '2', '1', '2', '2', '3', '3', '1', '2', '3', '1']
    3
    1
    2
    2
    3
    4

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

    score = {1:'44',
                 2:'66',
                 3:'78',
                 4:'67',
                 5:'88'
                }
    print(score)
    score[6] = '98'
    print(score)
    score[6] = '99'
    print(score)
    score.pop(6)
    print(score)
    
    for i in score:
        print("{:>2} : {:<2}".format(i,score.get(i)))

    输出结果:

    {1: '44', 2: '66', 3: '78', 4: '67', 5: '88'}
    {1: '44', 2: '66', 3: '78', 4: '67', 5: '88', 6: '98'}
    {1: '44', 2: '66', 3: '78', 4: '67', 5: '88', 6: '99'}
    {1: '44', 2: '66', 3: '78', 4: '67', 5: '88'}
     1 : 44
     2 : 66
     3 : 78
     4 : 67
     5 : 88

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

    s=list('456132798')
    t=set('4561327489')
    c={'小明':'1','小红':'3','小华':'2'}
    x=(1, 2, 3, 4, 5 )
    print('列表的遍历为:',end='')
    for i in s:
        print(i,end='')
    print()
    print('元祖的遍历为:',end='')
    for i in x:
        print(i,end='')
    print()
    print('字典key的遍历为:',end='')
    for i in c:
        print(i,end='')
    print()
    print('字典value的遍历为:',end='')
    for i in c.values():
        print(i,end='')
    print()
    print('数组的遍历为:',end='')
    for i in x:
        print(i,end='')

    输出结果:

    列表的遍历为:456132798
    元祖的遍历为:12345
    字典key的遍历为:小明小红小华
    字典value的遍历为:132
    数组的遍历为:12345

    4、英文词频统计实例

    1. 待分析字符串
    2. 分解提取单词
      1. 大小写 txt.lower()
      2. 分隔符'.,:;?!-_’
    3. 计数字典
    4. 排序list.sort()
    5. 输出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('
    ','')
    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])
  • 相关阅读:
    HTTPHelper
    C# 捕获全局亲测可用
    C# Excel的读写
    C# combox 绑定数据
    MySQL在CenterOS和Ubuntu的安装
    VMware虚拟机里的Centos7的IP
    Docker安装和部署
    linux安装git方法(转)
    mysql安装最后一步 Apply Security Settings 出错
    Linux下安装MySQL
  • 原文地址:https://www.cnblogs.com/xypbk/p/7563498.html
Copyright © 2011-2022 走看看