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

    1、字典

    d={'Tom':'01','Helen':'02','David':'03','Jim':'04'}
    增
    d['Wang']='05'
    删
    d.pop('Tom')
    改
    d['Tom']='06'
    查
    d['Tom']

    2、集合

    a=set('12345')
    b=set('56789')
    增
    a.add('6')
    删
    b.remove('9')
    并集
    a&b
    交集
    a|b
    a-b
    

      3、英文词频统计实例

    abc='''every night in my dreams
    i see you, i feel you,
    that is how i know you go on
    far across the distance
    and spaces between us
    you have come to show you go on
    near, far, wherever you are
    i believe that the heart does go on
    once more you open the door
    and you're here in my heart
    and my heart will go on and on
    love can touch us one time
    and last for a lifetime
    and never let go till we're one
    love was when i loved you
    one true time i hold to
    in my life we'll always go on
    near, far, wherever you are
    i believe that the heart does go on
    once more you open the door
    and you're here in my heart
    and my heart will go on and on
    there is some love that will not go away
    you're here, there's nothing i fear,
    and i know that my heart will go on
    we'll stay forever this way
    you are safe in my heart
    AND my heart will go on and on'''
    
    abc=abc.lower()
    for i in ',.':
        abc=abc.replace(i,' ')
    words=abc.split(' ')
    print(words)
    di{}
    words.sort()
    disc = set(words)
    for i in disc:
    di[i] = 0 for i in words: di[i] = di[i]+1 items = di.items() print(items)<br data-filtered="filtered"><br data-filtered="filtered"><br data-filtered="filtered">

      

      

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

       列表查找和插入的时间随元素的增加而增加,占用空间小,浪费内存很少;列表可以有N个元素,元素的类型是任意的,与列表本身无关。字典的查找和插入速度极快,不会随key的增加而变慢,需要占用大量内存,内存浪费多。字典使用键—值存储名具有极快的查找速度。字典的key必须是不可变对象。Python的集合(set)和其他语言类似, 是一个无序不重复元素集, 基本功能包括关系测试和消除重复元素. 集合对象还支持union(联合), intersection(交), difference(差)和sysmmetric difference(对称差集)等数学运算.由于集合是无序的,所以,sets 不支持 索引, 分片, 或其它类序列(sequence-like)的操作。

    4、列表,元组,字典,集合的遍历

    列表
    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)
    

      

  • 相关阅读:
    jquery关于attr和prop的差异
    C# Request.RawUrl与Request.Url的区别
    sqlserver 分区排序之partition
    asp.net core webapi iis jquery No 'Access-Control-Allow-Origin' header is present on访问跨域问题
    .netcore webapi iis 虚拟目录下载apk文件
    VSCode一直弹框错误Linter pylint is not installed
    c# ?. 空值传播运算符
    Angular5学习札记
    webstorm 打包angular Module build failed: Error: No PostCSS Config found
    js中被调用的函数获取调用者对象
  • 原文地址:https://www.cnblogs.com/Sun584125503/p/7573164.html
Copyright © 2011-2022 走看看