• 课程作业——字符串、组合数据类型练习


    1.字符串练习:

    strSource = 'http://news.gzcc.cn/html/2017/xiaoyuanxinwen_1027/8443.html'
    print(strSource[strSource.find('_') + 1:strSource.find('.html')])
    print('-+' * 50)
    
    strSource = 'https://docs.python.org/3/library/{}.html'
    lib = input('input lib name:')
    print(strSource.format(lib))
    print('-+' * 50)
    
    strSource = 'http://news.gzcc.cn/html/xiaoyuanxinwen/{}.html'
    for i in range(1, 5):
        print(strSource.format(i))
    print('-+' * 50)
    
    • 练习字符串内建函数:strip,lstrip,rstrip,split,count
    strSource = ' code that change world '
    print(strSource.split())  # 将strSource按照空格分割成由字符串组成的list
    print(strSource.split('t'))  # 将strSource按照空格分割成由字符串组成的list且字符串的首尾不包含't'
    
    print('|' + strSource.strip() + '|')  # 将strSource把首尾的空格去掉
    print('|' + strSource.strip('c') + '|')  # 将strSource把首尾的'c'去掉
    
    print('|' + strSource.lstrip() + '|')  # 将strSource把左边的空格去掉
    print('|' + strSource.lstrip('c') + '|')  # 将strSource把左边的'c'去掉
    
    print('|' + strSource.rstrip() + '|')  # 将strSource把右边的空格去掉
    print('|' + strSource.rstrip('d') + '|')  # 将strSource把右边的'd'去掉
    
    print(strSource.count('code'))  # 将strSource中'code'出现的次数统计出来
    print('-+' * 50)
    
    # 打印结果:
    # ['code', 'that', 'change', 'world']
    # [' code ', 'ha', ' change world ']
    # |code that change world|
    # | code that change world |
    # |code that change world |
    # | code that change world |
    # | code that change world|
    # | code that change world |
    # 1
    
    • 用函数得到校园新闻编号
    def getNewsURL(number):
        url = 'http://news.gzcc.cn/html/xiaoyuanxinwen/{}.html'
        print(url.format(number))
    
    
    getNewsURL(1)
    print('-+' * 50)
    
    • 用函数统计一歌词中单词出现的次数
    lyrics = '''
    Passion is sweet
    Love makes weak
    You said you cherised freedom so
    You refused to let it go
    Follow your faith 
    Love and hate
    never failed to seize the day
    Don't give yourself away
    Oh when the night falls
    And your all alone
    In your deepest sleep 
    What are you dreeeming of
    My skin's still burning from your touch
    Oh I just can't get enough 
    I said I wouldn't ask for much
    But your eyes are dangerous
    So the tought keeps spinning in my head
    Can we drop this masquerade
    I can't predict where it ends
    If you're the rock I'll crush against
    Trapped in a crowd
    Music's loud
    I said I loved my freedom too
    Now im not so sure i do
    All eyes on you
    Wings so true
    '''
    
    
    def getNumberOfWords(word, d_lyrics):
        list_of_lyrics = d_lyrics.split()
        return list_of_lyrics.count(word)
    
    
    print(getNumberOfWords('you', lyrics))
    print('-+' * 50)
    
    • 将字符串分解成一个个的单词。
     strSource = ' code that change world '
     print(strSource.split())
    
    

    2.组合数据类型练习

    • 分别定义字符串,列表,元组,字典,集合,并进行遍历。
    
    strSource = 'py'
    listSource = ['a', 'b']
    tupleSource = ('a', 'b')
    dictSource = {'key1': 'a', 'key2': 'b'}
    setSource = {'a', 'b'}
    for i in strSource:
        print(i)
    print('-+' * 50)
    for i in listSource:
        print(i)
    print('-+' * 50)
    for i in tupleSource:
        print(i)
    print('-+' * 50)
    for k, v in dictSource.items():
        print(k, v)
    print('-+' * 50)
    for i in setSource:
        print(i)
    print('-+' * 50)
    # 打印结果:
    # p
    # y
    # -+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
    # a
    # b
    # -+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
    # a
    # b
    # -+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
    # key1 a
    # key2 b
    # -+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
    # a
    # b
    # -+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
    
    • 总结列表,元组,字典,集合的联系与区别。
      • 列表:线性表,使用节点/元素来描述里面的单个数据,可增删查改
      • 元祖: 与列表相似,不可修改操作
      • 字典:使用key-value的形式保存数据,使用hash码生成key来确定存放位置
      • 集合:与字典相似,但是没有value,只有key
  • 相关阅读:
    mysql基础 MySql反向模糊查询
    mysql基础 函数
    html 标签的自定义属性应用
    mysql 分组后查询总行数,不使用子查询
    mysql基础 利用正则表达式判断数字
    网络工程师 教材目录
    Quatris
    BaseApplication Framework的skdCameraMan SdkTrayManager分析
    效率问题节点删除等
    ManulObject Ogre::RenderOperation::OT_TRIANGLE_STRIP
  • 原文地址:https://www.cnblogs.com/lger/p/8618495.html
Copyright © 2020-2023  润新知