码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2017-09-22 22:40:43      阅读:212      评论:0      收藏:0      [点我收藏+]

标签:可变   分解   字符   获取   after   定义   合数   art   date   

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

    a={‘1‘:90,‘2‘:89,‘3‘:88,‘4‘:98}
    print(‘字典‘,a)
    a[‘5‘]=99
    print(‘增加5学生成绩为99‘,a)
    a.pop(‘1‘)
    print(‘删除1的学生成绩‘,a)
    print(‘查找3的学生成绩‘,a.get(‘3‘))
    print(‘修改4的学生成绩为87‘,a)

  • 技术分享

     

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

    #列表
    l = list(‘23132111123132‘)
    #元组
    t = tuple(‘1233211232123‘)
    #字典
    d = dict(zip([1,2,3,4],[4,3,2,1]))
    #集合
    s = set(l)
    for i in l:
    print(i)
    for i in t:
    print(i)
    for i in s:
    print(i)
    for i in d:
    print(i)

  • 列表和元组是有序的,字典和集合都是无序的。

    列表用“[]”表示。列表是可变对象,它支持在原处修改的操作.也可以通过指定的索引和分片获取元素。

    元组用“()”表示。元组是只读的,不能修改。元组一旦定义其长度和内容都是固定的。

    字典用“{}”表示。字典存储键值对(key-value)数据。每一组用冒号连起来,然后各组用逗号隔开。

    集合没有特殊的表示方法,而是通过一个set函数转换成集合。集合内的元素没有重复的元素,主要是消除重复元素。

  • 英文词频统计实例
    1. 待分析字符串
    2. 分解提取单词
      1. 大小写 txt.lower()
      2. 分隔符‘.,:;?!-_’
      3. 单词列表
    3. 单词计数字典

      l=‘‘‘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
      I remember date and time
      September twenty second
      Sunday twenty five after nine
      In the doorway with your case
      No longer shouting at each other
      There were tears on our faces
      And we were letting go of something special
      Something we‘ll never have again
      I know, I guess I really really know
      Why do we never know what we‘ve got till it‘s gone
      How could I carry on the day you went away
      Cause I‘ve been missing you so much I have to say
      just one last dance‘‘‘
      print(‘待分析字符串‘,l)

      for i in ‘,.?!\n‘:
      l=l.replace(i,‘ ‘)
      l=l.lower()
      l=l.split(" ")
      print(‘单词计数词典‘)

      words=set(l)

      dt={}
      dt[‘day‘]=l.count(‘day‘)
      dt[‘you‘]=l.count(‘you‘)
      dt[‘so‘]=l.count(‘so‘)
      dt[‘me‘]=l.count(‘me‘)
      for j in words:
      dt[j]=l.count(j)
      for j in dt:
      print("{0:<11}{1}".format(j,dt[j]))
      print("{0:<11}{1}".format(j,dt[j]))

       运行结果:

      ================= RESTART: C:/Users/helloworld/Desktop/3.py =================
      待分析字符串 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
      I remember date and time
      September twenty second
      Sunday twenty five after nine
      In the doorway with your case
      No longer shouting at each other
      There were tears on our faces
      And we were letting go of something special
      Something we‘ll never have again
      I know, I guess I really really know
      Why do we never know what we‘ve got till it‘s gone
      How could I carry on the day you went away
      Cause I‘ve been missing you so much I have to say
      just one last dance
      单词计数词典
      day 5
      day 5
      you 9
      you 9
      so 3
      so 3
      me 4
      me 4
      2
      2
      other 1
      other 1
      about 1
      about 1
      no 1
      no 1
      with 1
      with 1
      crying 1
      crying 1
      i 12
      i 12
      second 1
      second 1
      this 1
      this 1
      there 1
      there 1
      lose 1
      lose 1
      again 1
      again 1
      time 2
      time 2
      really 4
      really 4
      longer 1
      longer 1
      only 1
      only 1
      dreaming 2
      dreaming 2
      well 1
      well 1
      at 1
      at 1
      could 1
      could 1
      is 1
      is 1
      each 1
      each 1
      september 1
      september 1
      there‘s 1
      there‘s 1
      something 2
      something 2
      special 1
      special 1
      last 1
      last 1
      shouting 1
      shouting 1
      were 3
      were 3
      to 4
      to 4
      be 1
      be 1
      we‘ll 1
      we‘ll 1
      call 2
      call 2
      what 1
      what 1
      we‘ve 1
      we‘ve 1
      did 1
      did 1
      one 1
      one 1
      faces 1
      faces 1
      for 1
      for 1
      i‘ve 1
      i‘ve 1
      tears 1
      tears 1
      doorway 1
      doorway 1
      the 6
      the 6
      like 1
      like 1
      missing 1
      missing 1
      got 1
      got 1
      someone 1
      someone 1
      case 1
      case 1
      she 1
      she 1
      letting 1
      letting 1
      better 1
      better 1
      we 2
      we 2
      baby 1
      baby 1
      love 2
      love 2
      sunday 1
      sunday 1
      away 3
      away 3
      know 4
      know 4
      does 1
      does 1
      true 1
      true 1
      dance 1
      dance 1
      just 1
      just 1
      still 1
      still 1
      hey 1
      hey 1
      but 1
      but 1
      carry 1
      carry 1
      remember 1
      remember 1
      blind 1
      blind 1
      go 1
      go 1
      need 1
      need 1
      never 2
      never 2
      when 1
      when 1
      say 2
      say 2
      crazy 1
      crazy 1
      been 3
      been 3
      after 2
      after 2
      since 2
      since 2
      it‘s 1
      it‘s 1
      all 1
      all 1
      suffering 1
      suffering 1
      stupid 1
      stupid 1
      sad 1
      sad 1
      in 1
      in 1
      our 1
      our 1
      guess 1
      guess 1
      much 2
      much 2
      of 3
      of 3
      went 3
      went 3
      your 1
      your 1
      nine 1
      nine 1
      on 2
      on 2
      my 1
      my 1
      cause 1
      cause 1
      was 1
      was 1
      twenty 2
      twenty 2
      and 3
      and 3
      do 4
      do 4
      lonely 1
      lonely 1
      date 1
      date 1
      have 2
      have 2
      five 1
      five 1
      gone 1
      gone 1
      how 1
      how 1
      till 1
      till 1
      why 1
      why 1

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

标签:可变   分解   字符   获取   after   定义   合数   art   date   

原文地址:http://www.cnblogs.com/Carrie-chong/p/7573277.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!