• 字符串练习、组合数据类型练习


    字符串练习:

    http://news.gzcc.cn/html/2017/xiaoyuanxinwen_1027/8443.html

    取得校园新闻的编号

    news="http://news.gzcc.cn/html/2017/xiaoyuanxinwen_1027/8443.html"
    print(news[-14:-5])

    https://docs.python.org/3/library/turtle.html

    产生python文档的网址

    addr1="https://docs.python.org/3/library/"
    addr2=".html"
    print(addr1+'turtle'+addr2)

    http://news.gzcc.cn/html/xiaoyuanxinwen/4.html

    产生校园新闻的一系列新闻页网址

    for i in range(10):
        print('http://news.gzcc.cn/html/xiaoyuanxinwen/{}.html'.format(i))

    练习字符串内建函数:strip,lstrip,rstrip,split,count

    用函数得到校园新闻编号

    addr="http://news.gzcc.cn/html/2017/xiaoyuanxinwen_1027/8443.html"
    print(news.rstrip('.html').split('_')[1])

    用函数统计一歌词中单词出现的次数

    song='''
    拦路雨偏似雪花
      饮泣的你冻吗
      这风褛我给你磨到有襟花
      连调了职也不怕
      怎么始终牵挂
      苦心选中今天想车你回家
      原谅我不再送花
      伤口应要结疤
      花瓣铺满心里坟场才害怕
      如若你非我不嫁
      彼此终必火化
      一生一世等一天需要代价
      谁都只得那双手
      靠拥抱亦难任你拥有
      要拥有必先懂失去怎接受
      曾沿着雪路浪游
      为何为好事泪流
      谁能凭爱意要富士山私有
      何不把悲哀感觉
      假设是来自你虚构
      试管里找不到它染污眼眸
      前尘硬化像石头
      随缘地抛下便逃走
      我绝不罕有
      往街里绕过一周
      我便化乌有
      情人节不要说穿
      只敢抚你发端
      这种姿态可会令你更心酸
      留在汽车里取暖
      应该怎么规劝
      怎么可以将手腕忍痛划损
      人活到几岁算短
      失恋只有更短
      归家需要几里路谁能预算
      忘掉我跟你恩怨
      樱花开了几转
      东京之旅一早比一世遥远
      谁都只得那双手
      靠拥抱亦难任你拥有
      要拥有必先懂失去怎接受
      曾沿着雪路浪游
      为何为好事泪流
      谁能凭爱意要富士山私有
      何不把悲哀感觉
      假设是来自你虚构
      试管里找不到它染污眼眸
      前尘硬化像石头
      随缘地抛下便逃走
      我绝不罕有
      往街里绕过一周
      我便化乌有
      谁都只得那双手
      靠拥抱亦难任你拥有
      要拥有必先懂失去怎接受
      曾沿着雪路浪游
      为何为好事泪流
      谁能凭爱意要富士山私有
      何不把悲哀感觉
      假设是来自你虚构
      试管里找不到它染污眼眸
      前尘硬化像石头
      随缘地抛下便逃走
      我绝不罕有
      往街里绕过一周
      我便化乌有
      你还嫌不够
      我把这陈年风褛
      送赠你解咒
    '''
    print(''+"出现"+str(song.count(''))+"")

    将字符串分解成一个个的单词

    article='''An old gentleman whose eyesight was failing came to stay in a hotel room with a bottle of wine in each hand.
     On the wall there was a fly which he took for a nail. So the moment he hung them on, the bottles fell broken and the wine
     spilt all over the floor. When a waitress discovered what had happened, she showed deep sympathy for him and decided to do him a 
     favour.
      So the next morning when he was out taking a walk in the roof garden, she hammered a nail exactly where the fly had stayed.
      Now the old man entered his room. The smell of the spilt wine reminded him of the accident. When he looked up at the wall, 
    he found the fly was there again! He walked to it carefully adn slapped it with all his strength. On hearing a loud cry, 
    the kind-hearted waitress rushed in. To her great surprise, the poor old man was there sitting on the floor, his teeth clenched 
    and his right hand bleeding!'''
    article=article.replace(","," ").replace("."," ").replace("  "," ").replace(""," ").replace("
    ","").split(" ")
    print(article)

     2.组合数据类型练习

    字符串遍历

    var1 = 'Hello World!'
    for i in var1:
        print(i)

    列表遍历

    var2 = ['Bob','Aoa','Coc']
    for i in var2:
        print(i)

    元组遍历

    tup1 = ('match','english','chinese')
    for i in tup1:
        print(i)

    字典遍历

    dict = {'Alice':'2341','Beth':'9102','Cecil':'3258'}

    for i in dict:
    print(i)
    for i in dict.keys():
    print(i)
    for i in dict.values():
    print(i)
    for i in dict.items():
    print(i)

    集合遍历

    ls = list('turtle')
    for i in ls: print(i)

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

    列表中的项目应该包括在方括号中,这样python就知道你是在指明一个列表。一旦你创建了一个列表,你就可以添加,删除,或者是搜索列表中的项目。

    元祖和列表十分相似,不过元组是不可变的。即你不能修改元组。

    字典把键和值联系在一起,键是唯一的不可修改,值可以修改

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

  • 相关阅读:
    Reverse Integer
    First Bad Version
    Heaters
    Number of Segments in a String
    RPI学习--环境搭建_串口连接
    关键字const
    main函数参数的使用
    Redis 简单使用 and 连接池(python)
    南宁AI项目
    Go part 1 初探
  • 原文地址:https://www.cnblogs.com/170he/p/8616388.html
Copyright © 2020-2023  润新知