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

时间:2021-02-18 00:03:35
    1. 字典实例:建立学生学号成绩字典,做增删改查遍历操作。
      dic={}
      dic[
      '01']='99'
      dic[
      '02']='84'
      dic[
      '03']='79'
      print('学生学号成绩字典:',dic)
      dic[
      '04']='75'
      print('增加:',dic)
      dic.pop(
      '04')
      print('删除:',dic)
      dic[
      '01']='94'
      print('修改:',dic)
      for i in dic:
      print('遍历:',i,dic[i])
    2. 列表,元组,字典,集合的遍历。
      list=['23143795']
      print('列表:',list)
      for i in list:
      print('列表的遍历:',i)


      t
      =('23143795')
      print('元组:',t)
      for i in t:
      print('元组的遍历:',i)


      s
      =['75','69','90']
      n
      =['a','b','c']
      ns
      =dict(zip(n,s))
      print('字典:',ns)
      for i in ns:
      print('字典的遍历:',i,ns[i])


      h
      =set([1,2,3])
      print('集合:',h)
      for i in h:
      print('集合的遍历:',i)

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

                         列表可重复,类型可不同,用“[]”表示;

                         元组元组是只读的,不能修改,元组用“()”表示;

                         字典存储键值对数据,字典最外面用大括号,每一组用冒号连接;

                         集合集合最好的应用是去重,通过一个set函数转换成集合。

                3.英文词频统计实例待分析字符串分解提取单词

  1. 大小写 txt.lower()
  2. 分隔符'.,:;?!-_’
  3. 单词列表
  4. 单词计数字典
    s='''It's been a long day, without you my friend

    And I'll tell you all about it when I see you again

    We've come a long way from where we began
    Oh I'll tell you all about it when I see you again
    When I see you again

    Damn who knew all the planes we flew
    Good things we've been through
    That I'll be standing right here
    Talking to you about another path
    I know we loved to hit the road and laugh
    But something told me that it wouldn't last
    Had to switch up look at things different see the bigger picture
    Those were the days hard work forever pays
    Now I see you in a better place

    How could we not talk about family when family's all that we got?
    Everything I went through you were standing there by my side
    And now you gonna be with me for the last ride
    It's been a long day without you my friend

    And I'll tell you all about it when I see you again

    We've come a long way from where we began
    Oh I'll tell you all about it when I see you again

    When I see you again

    '''
    s
    =s.lower()
    print('转化为小写:',s)

    for i in ',.':
    s
    =s.replace(i,' ')
    print('替换符号:',s)

    b
    =s.split(' ')
    print('分隔出一个一个的单词:',b)
    print('when出现的次数:',b.count('when'))
    print('my出现的次数:',b.count('my'))