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

时间:2023-02-13 18:31:40
  1. 字典实例:建立学生学号成绩字典,做增删改查遍历操作。
    d = {'Bob': 75, 'Michael': 95, 'Tracy': 85}
    #zeng
    d['Tom']=88
    print(d)
    #del
    d.pop('Michael')
    print(d)
    del(d['Tracy'])
    print(d)
    #gai
    d['Tom']=90
    print(d)
    #cha
    print(d.get('Bob'))

    print(d.keys())
    print(d.get('Michael',66))
    print(d.values())
    print(d.items())
    print(d.pop('Bob',55))

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

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

    l=list('1124564641321564')
    t
    =tuple('124856421574212')
    s
    =set('121542121213210')
    d
    =['Bob', 'Tom']
    i
    =[75, 90]
    di
    =dict(zip(d,i))

    for i in l:
    print(i,end='/')
    print()
    for i in t:
    print(i,end='/')
    print()
    for i in s:
    print(i,end='/')
    print()
    for i in di:
    print(i,di[i])

    组合数据类型练习,英文词频统计实例上                                                                                                                                                                                                                 列表,元组,字典,集合的联系与区别:

    1.列表,元组,字典是有顺序的,而集合是没顺序的

    2.列表是以[ ]形式表示,元组是以( )表示,字典以花括号表示,集合则是以[()]的形式表示

    3.列表是可变对象,它支持在原处修改的操作.也可以通过指定的索引和分片获取元素,区别于元组,可动态增加,删除,更新。

    4.元组和列表在结构上没有什么区别,唯一的差异在于元组是只读的,不能修改。元组用“()”表示。元组一旦定义其长度和内容都是固定的。一旦创建元组,则这个元组就不能被修改,即不能对元组进行更新、增加、删除操作。若想创建包含一个元素的元组,则必须在该元素后面加逗号“,”,否则创建的不是一个元组,而是一个字符串。

    5.集合没有特殊的表示方法,而是通过一个set函数转换成集合。集合是一个无序不重复元素集,基本功能包括关系测试和消除重复元素.。

    6.字典最大的价值是查询,通过键,查找值。

  3. 英文词频统计实例
    1. 待分析字符串
    2. 分解提取单词
      1. 大小写 txt.lower()
      2. 分隔符'.,:;?!-_’
      3. 单词列表
    3. 单词计数字典
      news='''President Xi * called on Monday on the whole country to raise up spirits and make persistent efforts to achieve the rejuvenation of the Chinese nation as the * Party of China is preparing for its 19th National Congress coming up in about three weeks.

      Xi, also general secretary of the * Party of China Central Committee, made the remark while visiting an exhibition at the Beijing Exhibition Hall. It is being held to showcase the country's progress in the past five years under the leadership of the CPC with Xi as the core.

      Since the Party's 18th National Congress held in November 2012, the CPC has led the people of the country to achieve long-term development of socialism with Chinese characteristics, improve the people’s livelihood, and produce historic reform of the Party and the country, Xi said.

      The past five years is an extraordinary period in the developmental progress of the Party and the country, he added.

      The exhibition, a precursor to the 19th National Congress of the CPC scheduled on Oct 18, has 10 sectors with different themes including boosting the rule of law, deepening reform, protecting the environment and building a strong military.
      '''

      #大小写
      news=news.lower()
      #去标点
      for i in ',.':
      news
      =news.replace(i,' ')
      print(news)
      #转为列表
      words=news.split(' ')
      print(words)

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