• 字符串练习


    字符串练习:

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

    取得校园新闻的编号

    addr = 'http://news.gzcc.cn/html/2018/xiaoyuanxinwen_0308/9005.html'
    print(addr[-14:-5])
    print(addr.rstrip('.html')[-9:])
    print(addr.rstrip('.html').split('_')[1])

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

    产生python文档的网址

    site1 = 'https://docs.python.org/3/library/'
    site2 = '.html'
    want = ['turtle', 'index', 'string']
    for i in want:
        print('https://docs.python.org/3/library/{}.html'.format(i))
        print(site1+i+site2)

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

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

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

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

    用函数得到校园新闻编号

    addr = 'http://news.gzcc.cn/html/2018/xiaoyuanxinwen_0308/9005.html'
    print(addr.rstrip('.html')[-9:]) print(addr.rstrip('.html').split('_')[1])

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

    歌曲《Welcome to New York》:分析Welcome出现次数

    song = '''
    Welcome to New York - Taylor Swift
    Walking through a crowd the village is aglow
    Kaleidoscope of loud heartbeats under coats
    Everybody here wanted something more
    Searching for a sound we hadn't heard before
    And it said
    Welcome to New York
    It's been waiting for you
    Welcome to New York
    Welcome to New York
    Welcome to New York
    It's been waiting for you
    Welcome to New York
    Welcome to New York
    It's a new soundtrack
    I can dance to this beat (beat)
    Forevermore
    The lights are so bright
    But they never blind me (me)
    Welcome to New York
    It's been waiting for you
    Welcome to New York
    Welcome to New York
    When we first dropped our bags on apartment floors
    Took our broken hearts put them in a drawer
    Everybody here was someone else before
    And you can want who you want
    Boys and boys and girls and girls
    Welcome to New York
    It's been waiting for you
    Welcome to New York
    Welcome to New York
    Welcome to New York
    It's been waiting for you
    Welcome to New York
    Welcome to New York
    It's a new soundtrack
    I can dance to this beat (beat)
    Forevermore
    The lights are so bright
    But they never blind me (me)
    Welcome to New York
    It's been waiting for you
    Welcome to New York
    Welcome to New York
    Like any great love it keeps you guessing
    Like any real love it's ever-changing
    Like any true love it drives you crazy
    But you know you wouldn't change anything anything anything
    Welcome to New York
    It's been waiting for you
    Welcome to New York
    Welcome to New York
    Welcome to New York
    It's been waiting for you
    Welcome to New York
    Welcome to New York
    It's a new soundtrack
    I can dance to this beat
    The lights are so bright
    But they never blind me
    Welcome to New York
    New soundtrack
    It's been waiting for you
    Welcome to New York
    The lights are so bright
    But they never blind me
    Welcome to New York
    So bright
    They never blind me
    Welcome to New York
    Welcome to New York
    '''
    word = 'Welcome'
    print(word+'出现'+str(song.count('Welcome'))+'')
    
    

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

    六级满分作文去掉标点符号后,分解成一个个单词,并了统计每个单词出现次数

    article = '''
        "avoid the rush-hours" must be the slogan of large cities the world over. whe rever you go, especially 
    at week-ends or on holidays, you'll find that the tr ains a re packed, streets crowded, buses queued, restaurant 
    tables shared. moreover, the smallest unforeseen event like a power-cut, an exceptionally heavy snowfall or rainfall 
    can bring about conditions of utterinconvenience and chaos.
       city-dwellers are obliged by their environment to adopt 
    a wholly unnatural way of life. theylose touch with nature and all the simple, good things of life like sunshine and 
    f resh air atapremium . tall buildings block out the sun. traffic fumes pollute the atmosphere. the flow oftraffic 
    goes on unceasingly and the noise never stops.
       the funny thing about it all in a large modern city is that you pay 
    dearly for the "privilege" ofliving. the demand for accommodation is so great that it is of ten impossible for 
    ordinarypeople to buy a house/flat of their own. furthermore, the cost of living is very high. just abouteve rything 
    you buy is likely to be more expensive than it would be in the country.
       in addition, city-dwellers live under 
    constant threat. the crime rate in most cities is very high. houses are often burgled. cities breed crime and 
    violence and are full of places you would beafr aid to visit at night. if you think about it, they are not really fit 
    to live in at all. can anyonereally doubt that the country is what man was born for and where he truly belongs? '''
    replace = ['"', '.', '?', ',', '(', ')']
    for i in replace:
        article = article.replace(i, ' ')
    artarray = article.split()
    print(artarray)
    for i in artarray:
        print(i+'出现'+str(article.count(i))+'')

    2.组合数据类型练习

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

    字符串:

    string = 'string'
    for i in string:
        print(i)

    列表:

    ls = list(range(5))
    ls2 = list('turtle')
    ls3 = ['h', 'e', 'l', 'l', 'o']
    for i in ls:
        print(i)
    for i in ls2:
        print(i)
    for i in ls3:
        print(i)

    元组:

    ls1 = list(range(5))
    ls2 = list('turtle')
    tuple1 = tuple(ls1)
    tuple2 = tuple(ls2)
    tuple3 = ('h', 'e', 'l', 'l', 'o')
    for i in tuple1:
        print(i)
    for i in tuple2:
        print(i)
    for i in tuple3:
        print(i)

    字典:

    dict = dict(zip('turtle', '123456'))
    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')
    set = set(ls)
    for i in set:
        print(i)

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

    联系:

      元组、字典、集合都可以通过预先定义列表来进行创建;

      并且元组中可以含有列表、列表中也可以包含元组;

      元组中的列表的元素可以修改;

      集合中的元素就是字典中的键值;

      列表,元组,字典,集合之间可以相互转化。

    区别:

      元组和列表是有序列的,字典和集合是无序列的;

      元组中除了列表元素里的元素可修改以外,其他不可以修改;

      字典中的键不可以修改,值可以修改;

      元组不能使用列表的方法增加自身的元素,集合可以使用add()方法添加元素;

      元组和列表的元素可以重复,字典和集合的元素不能重复。

  • 相关阅读:
    php json_encode JSON_UNESCAPED_UNICODE
    ubuntu 添加多个ssh公钥和私钥
    如何自动化新增配置文件呢?
    git配置ssh公钥
    项目PHP新知识点
    mysql 数据库复制表 create table city1 like city;
    .NET 泛型集合数据写CSV文件
    .NET C# 泛型队列
    逆向地理编码--根据地址搜索定位,点击地图、获取经纬度信息
    正向地理编码-根据输入地址获取经纬度
  • 原文地址:https://www.cnblogs.com/171-LAN/p/8612112.html
Copyright © 2020-2023  润新知