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


    1.字符串练习:

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

    取得校园新闻的编号

    方法一:

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

    方法二:

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

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

    产生python文档的网址

    add1='https://docs.python.org/3/library/'
    add2=['turtle','index']
    add3='.html'
    for i in add2:
        print(add1+i+add3)

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

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

    for i in range(1, 20):
        s = 'http://news.gzcc.cn/html/xiaoyuanxinwen/{}.html'.format(i)
        print(s)

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

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

    s='this is string example...   '
    s1='wow'
    print(s+s1)
    print(s.lstrip('this'))
    print(s.rstrip('...   '))
    print(s.strip()+s1)
    print(s.split())
    print(s.split('i'))
    print(s.count('s'))

    用函数得到校园新闻编号

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

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

    str='''Heartbeats  Amy Diamond
    I can't figure out
    Is it meant to be this way
    Easy words so hard to say
    I can't live without
    Knowing how you feel
    Know if this is real
    Tell me am I mistaken
    Cause I don't have another heart for breakin'
    Please don't let me go
    I just wanna stay
    Can't you feel my heartbeats
    Giving me away
    I just want to know
    If you too feel afraid
    I can feel your heartbeats
    Giving you away
    Giving us away
    I can't understand
    How it's making sense
    That we put up such defense
    When all you need to know
    No matter what you do
    I'm just as scared as you
    Tell me am I mistaken
    Cause I don't have another heart for breakin'
    Please don't let me go
    I just wanna stay
    Can't you feel my heartbeats
    Giving me away
    I just want to know
    If you too feel afraid
    I can feel your heartbeats
    Giving you away
    Giving us away
    Please don't let me go
    I just wanna stay
    Can't you feel my heartbeats
    Giving me away
    I just want to know
    If you too feel afraid
    I can feel your heartbeats
    Giving you away
    Giving us away'''
    print(str.count('away'))

    2.组合数据类型练习

    分别定义字符串,列表,元组,字典,集合,并进行遍历。

    字符串:
    s='turtle'
    for i in s:
        print(i)
    列表:
    classmates=['Jenny','Lucy','Tidy','Alice',[1,2,3,]]
    for i in classmates:
        print(i)
    元祖:
    tup=tuple(classmates)
    tup[-1][1]=0
    for i in tup :
        print(i)
    字典:
    d={'Bob':75,'Tracy':95,'Lisa':88}
    for i in d:
        print(i)
    集合:
    s1={1,2,3,4,5,}
    s2={1,3,6,9}
    s=s1&s2
    print(s)
    for i in s:
        print(i)

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

    总结:列表和元祖都是有序的序列。元祖和列表非常相似,但是元祖不可修改,但是若元祖的某一个元素属性为列表,则该列表元素中的元素可以修改。字典和集合是无序的、不重复的。字典使用键-值(key-value)对存储,key是不可变对象。集合是一组key的集合,但不存储value。列表的元素集合用中括号,元祖用圆括号,字典和集合用大括号。

  • 相关阅读:
    引用数据类型:字符串和数组
    java流程控制
    java基本类型-八大基本数据类型(4类8种)
    Java标识符
    Java注释
    虚拟环境管理virtualenv
    pipenv管理模块和包
    有关线程的说法?
    TCP三次握手的序列号和确认号的计算
    jenkins的安装
  • 原文地址:https://www.cnblogs.com/ffde/p/8618581.html
Copyright © 2020-2023  润新知