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

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

    dic={"aa":"90","bb":"91","cc":"92","dd":"94","ee":"95"}
    print("查询aa的成绩")
    print(dic.get('aa'))
    print("删除bb成绩")
    dic.pop('bb')
    print("bb的成绩:","bb" in dic,"不存在")
    print("aa成绩改为80")
    dic['aa']=80
    print(dic.items())
    print("添加gg的成绩")
    dic["gg"]="99"
    print(dic)
    print("所有学生名单:")
    for i in dic:
    print(i)
    print("所有学生成绩:")
    for i in dic:
    print(i,dic[i])

    查询aa的成绩
    90
    删除bb成绩
    bb的成绩: False 不存在
    aa成绩改为80
    dict_items([('aa', 80), ('cc', '92'), ('dd', '94'), ('ee', '95')])
    添加gg的成绩
    {'aa': 80, 'cc': '92', 'dd': '94', 'ee': '95', 'gg': '99'}
    所有学生名单:
    aa
    cc
    dd
    ee
    gg
    所有学生成绩:
    aa 80
    cc 92
    dd 94
    ee 95
    gg 99

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

    lis=["1","2","3","4"]
    tu=("4","5")
    dic={"1":"91","2":"92","3":"93","4":"94"}
    s=set(lis)
    print("列表")
    for i in lis:
    print(i)
    print("元组")
    for i in tu:
    print(i)
    print("字典")
    for i in dic:
    print(i,dic[i])
    print("set")
    for i in s:
    print(i)

    列表
    1
    2
    3
    4
    元组
    4
    5
    字典
    1 91
    2 92
    3 93
    4 94
    set
    3
    2
    1

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

    (1)列表是任意对象的序列。列表用方括号表示。

    (2)将一组值打包到一个对象中,称为元组。元组用圆括号表示。元组和列表的大部分操作相同。但是,列表是不固定的,可以随时插入,删除;而元组一旦确认就不能够再更改。所以,系统为了列表的灵活性,就需要牺牲掉一些内存;而元组就更为紧凑。

    (3)与列表和元组不同,集合是无序的,也不能通过索引进行访问。此外,集合中的元素不能重复。

    (4)字典就是一个关联数组或散列表,其中包含通过关键字索引的对象。用大括号表示。与集合相比,通过关键字索引,所以比集合访问方便。字典是Python解释器中最完善的数据类型。

    3.英文词频统计实例

    A. 待分析字符串  

    B. 分解提取单词

     a. 大小写 txt.lower()  b. 分隔符'.,:;?!-_’    c.单词列表

    s='''Twinkle, twinkle, little star,
    
    How I wonder what you are.
    
    Up above the world so high,
    
    Like a diamond in the sky.
    
    Twinkle, twinkle, little star,
    
    How I wonder what you are!
    
    When the blazing sun is gone,
    
    When he nothing shines upon,
    
    Then you show your little light,
    
    Twinkle, twinkle, all the night.
    
    Twinkle, twinkle, little star,
    
    How I wonder what you are!
    
    Then the traveler in the dark
    
    Thanks you for your tiny spark;
    
    He could not see which way to go,
    
    If you did not twinkle so.
    
    Twinkle, twinkle, little star,
    
    How I wonder what you are!
    
    Twinkle Twinkle Little Star'''
    s=s.lower()
    print('大写转换为小写:'+s)
    for i in ',.!':
        s=s.replace(i,' ')
    print('所有标点符号替换为空格:'+s)
    print('单词列表及词频:')
    words=s.split(' ')
    dic={}
    words.sort()
    disc=set(words)
    for i in disc:
        dic[i] = 0
    for i in words:
        dic[i] = dic[i]+1
    items = dic.items()
    print(items)
    大写转换为小写:twinkle, twinkle, little star,
    
    how i wonder what you are.
    
    up above the world so high,
    
    like a diamond in the sky.
    
    twinkle, twinkle, little star,
    
    how i wonder what you are!
    
    when the blazing sun is gone,
    
    when he nothing shines upon,
    
    then you show your little light,
    
    twinkle, twinkle, all the night.
    
    twinkle, twinkle, little star,
    
    how i wonder what you are!
    
    then the traveler in the dark
    
    thanks you for your tiny spark;
    
    he could not see which way to go,
    
    if you did not twinkle so.
    
    twinkle, twinkle, little star,
    
    how i wonder what you are!
    
    twinkle twinkle little star
    所有标点符号替换为空格:twinkle  twinkle  little star 
    
    how i wonder what you are 
    
    up above the world so high 
    
    like a diamond in the sky 
    
    twinkle  twinkle  little star 
    
    how i wonder what you are 
    
    when the blazing sun is gone 
    
    when he nothing shines upon 
    
    then you show your little light 
    
    twinkle  twinkle  all the night 
    
    twinkle  twinkle  little star 
    
    how i wonder what you are 
    
    then the traveler in the dark
    
    thanks you for your tiny spark;
    
    he could not see which way to go 
    
    if you did not twinkle so 
    
    twinkle  twinkle  little star 
    
    how i wonder what you are 
    
    twinkle twinkle little star
    单词列表及词频:
    dict_items([('', 10), ('shines', 1), ('sky', 1), ('world', 1), ('you', 7), ('twinkle', 8), ('in', 2), ('night', 1), ('upon', 1), ('dark
    
    thanks', 1), ('for', 1), ('what', 4), ('your', 2), ('a', 1), ('
    
    how', 4), ('so', 2), ('above', 1), ('diamond', 1), ('show', 1), ('
    
    if', 1), ('all', 1), ('could', 1), ('
    
    when', 2), ('
    
    up', 1), ('way', 1), ('nothing', 1), ('high', 1), ('star', 5), ('light', 1), ('spark;
    
    he', 1), ('is', 1), ('traveler', 1), ('which', 1), ('wonder', 4), ('did', 1), ('tiny', 1), ('little', 6), ('
    
    twinkle', 5), ('go', 1), ('blazing', 1), ('the', 6), ('he', 1), ('i', 4), ('see', 1), ('are', 4), ('not', 2), ('sun', 1), ('to', 1), ('
    
    then', 2), ('
    
    like', 1), ('gone', 1)])
  • 相关阅读:
    截取某一个元素的图
    11、python的异常机制
    10、python面向对象三大特性:封装、继承、多态
    9、python之面向对象
    软件测试有前途吗?
    对应届生做测试的同学们的一些建议
    没有代码基础如何学习自动化测试?
    接口自动化测试有哪些工具或者框架?
    软件测试流程
    接口自动化测试中logging实际用法
  • 原文地址:https://www.cnblogs.com/00js/p/7595950.html
Copyright © 2011-2022 走看看