• 字符串练习


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

    取得校园新闻的编号

    >>> s="http://news.gzcc.cn/html/2017/xiaoyuanxinwen_1027/8443.html"
    >>> s[45:54]
    '1027/8443'

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

    产生python文档的网址

    >>> s1="http://docs.python.org/3/library/"
    >>> s2="turtle.html"
    >>> s=s1+s2
    >>> print(s)
    http://docs.python.org/3/library/turtle.html
    >>>

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

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

    for i in range(2,231):

    "http://news.gzcc.cn/html/xiaoyuanxinwen/{}.html".format(i)

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

    用函数得到校园新闻编号

    方法一:

    str="http://news.gzcc.cn/html/2017/xiaoyuanxinwen_1027/8443.html"

    >>> str.replace('_','.').split('.')[3]

     '1027/8443'

    方法二:

    >>> str = 'http://news.gzcc.cn/html/2018/xiaoyuanxinwen_1027/8443.html'

    >>> print(str.rstrip('.html')[-9:])
    1027/8443

    方法三:

    >>> str = 'http://news.gzcc.cn/html/2018/xiaoyuanxinwen_1027/8443.html'

    >>> print(str.rstrip('.html').split('_')[1])
    1027/8443

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

    >>> mus="""
    I'm gonna stand right here
    I'm not gonna lose
    But when the sky falls down
    You'll find me next to you
    I'm gonna stand right here
    And wait for you voice
    But when the worlds
    You know we got no choice, hey
    When you hear the sound
    Of the world of war
    You know that you can see is a big red sky
    When you know you can't make it on your own
    I will be your soldier
    When you falling down and your on the fence
    I will be your shield that will keep you save
    When you know you can't make it on your own
    I will be your soldier
    When you hear the sound
    Of the world of war
    You know that you can see is a big red sky
    When you know you can't make it on your own
    I will be your soldier
    When you falling down and your on the fence
    I will be your shield that will keep you save
    When you know you can't make it on your own
    I will be your soldier
    I will be your soldier
    I will be your soldier"""


    >>> mus.count('soldier')
    6

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

    >>> str="http://news.gzcc.cn/html/xiaoyuanxinwen/9.html"


    >>> str.split('/')
    ['http:', '', 'news.gzcc.cn', 'html', 'xiaoyuanxinwen', '9.html']


    >>> str.split('/',1)
    ['http:', '/news.gzcc.cn/html/xiaoyuanxinwen/9.html']
    >>>

  • 相关阅读:
    bzoj 1012: [JSOI2008]最大数maxnumber 线段树
    Codeforces Round #260 (Div. 2) A , B , C 标记,找规律 , dp
    Codeforces Round #256 (Div. 2) E. Divisors 因子+dfs
    Codeforces Round #340 (Div. 2) E. XOR and Favorite Number 莫队算法
    Codeforces Round #348 (VK Cup 2016 Round 2, Div. 1 Edition) C. Little Artem and Random Variable 数学
    BZOJ 1005 [HNOI2008]明明的烦恼 purfer序列,排列组合
    BZOJ 1211: [HNOI2004]树的计数 purfer序列
    UVA 1629 Cake slicing 记忆化搜索
    UVA1630 Folding 区间DP
    BNU 51640 Training Plan DP
  • 原文地址:https://www.cnblogs.com/XiaoFengLuo/p/8617515.html
Copyright © 2020-2023  润新知