zoukankan      html  css  js  c++  java
  • 9.20作业

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

           列表,元组,字典,集合的联系与区别:列表,元组是有顺序的,而字典和集合是没顺序的。列表是以[ ]形式表示,元组是以( )表示,字典以{ }表示,集合则是以[()]的形式表示。列表是可变对象,可以有增删改操作,而元组是只读的,不能修改。字典使用键-值(key-value)存储,键是不可变的对象。插入和查找速度快,不会随着键的增加而变慢,需要占用大量的内存。字典是用空间换取时间的一种方法。集合是一个无序不重复元素集, 基本功能包括关系测试和消除重复元素。

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

    列表是可变序列,元组是不可变序列;列表的值可以修改,而元祖的值初始化后不可修改,两者都是有序的。字典和集合 两者都是无序的,数据量大时可用集合或列表来创建
    a = list('457451')      #列表的遍历
    print(a)
    for i in a:
        print(i)
    
    b = tuple('485890')       #元组的遍历
    print(b)
    for i in b:
        print(i)
    
    c = set('256156')        #集合的遍历
    print(c)
    for i in c:
        print(i)
    
    d = {'bob':80,'rose':79,'jack':90}        #字典的遍历
    print(d)
    for i in d:
        print(i,d[i])

     

    英文词频统计:

    • 下载一首英文的歌词或文章str
    • 分隔出一个一个的单词 list
    • 统计每个单词出现的次数 dict
    strgc='''
    Oh, nowhere left to go.
    Are we getting closer
    No. All we know is No.
    Nights are getting colder, colder
    Hey. Tears all fall the same.
    We all feel the rain.
    We can't change.
    Everywhere we go, we're looking for the sun.
    Nowhere to grow old. We're always on the run.
    They say we'll rot in hell, but I don't think we will.
    They've branded us enough. Outlaws of love.
    Scars make us who we are.
    Hearts and homes are broken, broken.
    Far, we could go so far,with our minds wide open, open.
    Hey. Tears all fall the same.
    We all feel the rain.
    We can't change.
    Everywhere we go, we're looking for the sun.
    Nowhere to grow old. We're always on the run.
    They say we'll rot in hell, but I don't think we will.
    They've branded us enough. Outlaws of love.
    Outlaws of love.'''
    
    strgc = strgc.replace('.',' ')
    strList = strgc.split()
    print(len(strList),strList)    #分隔一个一个单词并统计英文单词个数
    strSet = set(strList)      #将列表转化成集合,再将集合转化成字典来统计每个单词出现次数
    print(strSet)
    strDict={}
    for word in strSet:
        strDict[word] = strList.count(word)
    print(len(strDict),strDict)

  • 相关阅读:
    获得 Web Service 方法的描述信息
    make menuconfig 报错
    汇编调用c函数为什么要设置栈
    UBoot Makefile文件分析
    UBoot启动过程完全分析(转)
    (转)在fedora12下用crosstoolng建立armlinux交叉编译环境
    UBoot编译过程完全分析(转)
    雷军:给互联网创业者的“七字”建议
    uboot根目录下makefile
    Redhat 5 配置Samba服务器
  • 原文地址:https://www.cnblogs.com/Tlzlykc/p/9679899.html
Copyright © 2011-2022 走看看