zoukankan      html  css  js  c++  java
  • python:零散记录

    1、rstrip()删除末尾指定字符串

    例如:A = '1,2,3,4,5,'

       B = A.rstrip(',')

       B = '1,2,3,4,5'

    2、isdigit()方法

    Python isdigit() 方法检测字符串是否只由数字组成,返回 True 或者False。

    例如:str = '123456'

       str.isdigit()为True

    3、enumerate() 函数

    enumerate() 函数用于将一个可遍历的数据对象(如列表、元组或字符串)组合为一个索引序列,同时列出数据和数据下标,一般用在 for 循环当中。

    例如:seq = ['one', 'two', 'three']

       for i, element in enumerate(seq):

         print i, element

    输出:0 one

       1 two

       2 three

    4、字符串 startswith、find、join

    例如:name = 'Swaroop'

       if name.startswith('Swa'):  #startswith方法用于查找字符串是否以给定的字符串内容开头

        print('the string start with "Swa"')

       if 'a' in name:

        print('it contains the string "a"')

       if name.find('war') !=-1:  #find方法用于定位字符串中给定字符串的位置,如果找不到对应的字符串,会返回-1

        print('it contains the string "war"')

       delimiter = '_*_'

       mylist = ['Brazil','Russia','India','China']

       print(delimiter.join(mylist))  #字符串delimiter将会作为每个项目之间的分隔,并以此返回一串更大的字符串

    输出:the string start with "Swa"

       it contains the string "a"

       it contains the string "war"

       Brazil_*_Russia_*_India_*_China

     5、函数传递元组,返回多个变量

    例如:def run():

        return ('hello','world')

       first, second = run()

       print(first)

       print(second)

    输出:hello

       world

    6、在函数中接收元组与字典

    分别使用 * 或 ** 作为元组或字典的前缀,来使它们作为一个参数为函数所接收

    例如:def powersum(power, *args):

        total = 0

        for i in args:total += pow(i, power)

        return total

       print(powersum(2, 3, 4))

       print(powersum(2, 10))

    输出:25

       100

  • 相关阅读:
    CodeForces Round #516 Div2 题解
    BZOJ3300: [USACO2011 Feb]Best Parenthesis 模拟
    BZOJ4994: [Usaco2017 Feb]Why Did the Cow Cross the Road III 树状数组
    BZOJ3297: [USACO2011 Open]forgot DP+字符串
    BZOJ3296: [USACO2011 Open] Learning Languages 并查集
    BZOJ2442: [Usaco2011 Open]修剪草坪 单调队列优化dp
    BZOJ3298: [USACO 2011Open]cow checkers 威佐夫博弈
    什么是 DDoS 攻击?
    快速了解“云原生”(Cloud Native)和前端开发的技术结合点
    一文读懂spring boot 和微服务的关系
  • 原文地址:https://www.cnblogs.com/ywxbbbbb/p/10103449.html
Copyright © 2011-2022 走看看