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


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

    >>> d={'张三':'88','李四':'84','王五':'66','何六':'78'}
    >>> d['张三']
    '88'
    >>> '陈七' in d
    False
    >>> d['陈七']='94'
    >>> d
    {'张三': '88', '李四': '84', '王五': '66', '何六': '78', '陈七': '94'}
    >>> d.pop('张三')
    '88'
    >>> del(d['李四'])
    >>> d
    {'王五': '66', '何六': '78', '陈七': '94'}
    >>> d.keys()
    dict_keys(['王五', '何六', '陈七'])
    >>> d.values()
    dict_values(['66', '78', '94'])
    >>> d.items()
    dict_items([('王五', '66'), ('何六', '78'), ('陈七', '94')])

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

    >>> ls=list('201506050082')
    >>> ls
    ['2', '0', '1', '5', '0', '6', '0', '5', '0', '0', '8', '2']
    >>> tu=tuple('201506050082')
    >>> tu
    ('2', '0', '1', '5', '0', '6', '0', '5', '0', '0', '8', '2')
    
    >>> se=set('201506050082')
    >>> se
    {'5', '6', '8', '0', '1', '2'}
    >>> d={'tom':'66','rose':'55','jack':'78'}
    >>> d
    {'tom': '66', 'rose': '55', 'jack': '78'}
    >>> for i in ls:
        print(i)
    
        
    2
    0
    1
    5
    0
    6
    0
    5
    0
    0
    8
    2
    >>> for i in se:
        print(i)
    
        
    5
    6
    8
    0
    1
    2
    >>> for i in d:
        print(i)
    
        
    tom
    rose
    jack
    >>> for i in d:
        print(i,d[i])
    
        
    tom 66
    rose 55
    jack 78

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

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

    元祖
    元组是只读的,不能修改
    不能对元组进行更新、增加、删除操作。

    集合

    是一个无序不重复元素集,基本功能包括关系测试和消除重复元素.。

    字典
    字典存储键值对数据。字典最外面用大括号,每一组用冒号连起来,然后各组用逗号隔开。字典最大的价值是查询,通过键,查找值。

    3.英文词频统计实例

    s='''Hey Jude, don't make it bad.
    Take a sad song and make it better.
    Remember to let her into your heart,
    Then you can start to make it better.
    Hey Jude, don't be afraid.
    You were made to go out and get her.
    The minute you let her under your skin,
    
    Then you begin to make it better.
    And anytime you feel the pain, hey Jude, refrain,
    Don't carry the world upon your shoulders.
    For well you know that it's a fool who plays it cool
    By making his world a little colder.
    Hey Jude, don't let me down.
    You have found her, now go and get her.
    Remember to let her into your heart,
    Then you can start to make it better.
    So let it out and let it in, hey Jude, begin,
    You're waiting for someone to perform with.
    And don't you know that it's just you, hey Jude, you'll do,
    The movement you need is on your shoulder.
    Hey Jude, don't make it bad.
    Take a sad song and make it better.
    Remember to let her under your skin,
    Then you'll begin to make it
    Better better better better better better, oh.
    Na na na, na na na na, na na na, hey Jude
    Na na na, na na na na, na na na, hey Jude
    Na na na, na na na na, na na na, hey Jude'''
    
    s=s.lower()
    for i in ',!':
        s=s.replace(i,' ')
    s=s.split(' ')
    print(s)
    
    dict={}
    for i in s:
        dict[i]=s.count(i)
    print(dict)

  • 相关阅读:
    springboot日志框架
    springboot创建一个可执行的jar
    springboot整合Thymeleaf模板引擎
    springboot自定义SpringApplication启动类
    springboot配置mybatis的mapper路径
    使用@SpringBootApplication注解
    HDU1269 迷宫城堡 —— 强连通分量
    POJ3177 Redundant Paths —— 边双联通分量 + 缩点
    HDU3394 Railway —— 点双联通分量 + 桥(割边)
    UVA796 Critical Links —— 割边(桥)
  • 原文地址:https://www.cnblogs.com/hxl316/p/7595957.html
Copyright © 2020-2023  润新知