• 9.20作业


    总结列表,元组,字典,集合的联系与区别:列表是以[ ]形式表示,元组是以( )表示,字典以{ }表示,集合则是以[()]的形式表示。列表和元组都是有序的,集合和字典是无序的。元组只可读,其他都可读写。字典是以键值对存储。集合中的元素不能重复,列表、元组和字典的元素均可重复。

    列表

    name = ['Linda', 'Lucy', '狗剩', '皮蛋', '狗剩']
    print(name)
    
    print(name[0])
    
    len(name)
    name.index('狗剩')
    name[1]='Troy'
    print(name)
    
    name.sort()
    print(name)
    
    name.insert(1,'二蛋')
    print(name)
    
    name.append('狗屎')
    print(name)
    
    name.pop(1)
    print(name)
    
    del name[-1]
    print(name)
    

      运行结果

     

    集合

    s = {1, 2, 3}
    print(s)
    
    s.add(4)
    print(s)
    
    s.remove(2)
    print(s)
    
    s = set([1, 1, 2, 2, 3, 3])
    print(s)
    
    s1 = {1, 2, 3}
    s2= {2, 3, 4}
    print(s1&s2)
    print(s1|s2)
    print(s2-s1)
    

      运行结果

    英文歌词

    英文词频统计:

    • 下载一首英文的歌词或文章str
    • 分隔出一个一个的单词 list
    • 统计每个单词出现的次数 dict
    strhello='''
    Hello, it's me, I was wondering
    If after all these years you'd like to meet to go over everything
    They say that time's supposed to heal, yeah
    But I ain't done much healing
    
    Hello, can you hear me?
    I'm in California dreaming about who we used to be
    When we were younger and free
    I've forgotten how it felt before the world fell at our feet
    
    There's such a difference between us
    And a million miles
    
    Hello from the other side
    I must've called a thousand times 
    To tell you I'm sorry, for everything that I've done
    But when I call you never seem to be home
    
    Hello from the outside
    At least I can say that I've tried 
    To tell you I'm sorry, for breaking your heart
    But it don't matter, it clearly doesn't tear you apart anymore
    
    Hello, how are you?
    It's so typical of me to talk about myself, I'm sorry
    I hope that you're well
    Did you ever make it out of that town where nothing ever happened?
    
    It's no secret
    That the both of us are running out of time
    
    So hello from the other side
    I must've called a thousand times 
    To tell you I'm sorry, for everything that I've done
    But when I call you never seem to be home
    
    Hello from the outside
    At least I can say that I've tried 
    To tell you I'm sorry, for breaking your heart
    But it don't matter, it clearly doesn't tear you apart anymore
    
    Ooh, anymore
    Ooh, anymore
    Ooh, anymore
    Anymore...
    
    Hello from the other side
    I must've called a thousand times 
    To tell you I'm sorry, for everything that I've done
    But when I call you never seem to be home
    
    Hello from the outside
    At least I can say that I've tried 
    To tell you I'm sorry, for breaking your heart
    But it don't matter, it clearly doesn't tear you apart anymore'''
    
    strgc = strhello.replace('.',' ')
    strList = strhello.split()
    print(len(strList),strList)    #分隔一个单词并统计英文单词个数
    strSet = set(strList)      #将列表转化成集合,再将集合转化成字典来统计每个单词出现次数
    print(strSet)
    strDict={}
    for hello in strSet:
        strDict[hello] =strList.count(hello)
    print(strDict,len(strDict))

    运行结果

  • 相关阅读:
    使用springamqp发送消息及同步接收消息
    对未登陆的用户进行处理的页面
    查找某些字符是否在另一个字符串里出现的高效算法
    正则表达式
    华中地区高校第七届ACM程序设计大赛——递增序列【2012年5月27日】
    HDOJ2021 ( 发工资咯:) ) 【水题】
    循环冗余校验(CRC)【C语言 位运算】
    HDOJ2028 ( Lowest Common Multiple Plus ) 【水题,lcm】
    HDOJ2015 ( 偶数求和 ) 【水题】
    HDOJ2027 ( 统计元音 ) 【水题】
  • 原文地址:https://www.cnblogs.com/fanfanfan/p/9689942.html
Copyright © 2020-2023  润新知