zoukankan      html  css  js  c++  java
  • python的几个常用内置函数

    dir()查看属性(函数和数据对象)

    help()查看具体的帮助文档

    id() 用来查看数据对象的地址

    split 分隔(str ---> list):

    >>> s="my:name:is:xiaofan"
    >>> s.split(":")
    ['my', 'name', 'is', 'xiaofan']

    join合并(list--->str)

    >>> l1=["a","b","c"]
    >>> l2='-'.join(l1)
    >>> print(l2)
    a-b-c

    去除(左右两边)空格、制表符等:

    >>> name = " xaio fan "
    >>> name.strip(' ')
    'xaio fan'
    >>> name.rstrip(' ')
    ' xaio fan'
    >>> name.lstrip(' ')
    'xaio fan '

    replace替换:

    >>> name
    ' xaio fan '
    >>> name.replace(' ','')
    'xaiofan'

    count 计算出现的个数:

    >>> name
    ' xaio fan '
    >>> name.count('x')
    1

    find、index 查找字符的索引的位置(区别:index查找不到会报错)

    >>> name
    ' xaio fan '
    >>> name.find('i')
    3
    >>> name="this is a boy"
    >>> name.find('a')
    8
    >>> name.index('a')
    8
    >>> name.find('e')
    -1
    >>> name.index('e')
    Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
    ValueError: substring not found

    zip 合并列表:

    >>> a=[1,2,3]
    >>> b=[1,2,3]

    >>> zip(a,b)
    [(1, 1), (2, 2), (3, 3)]

    >>> dict(zip(a,b))
    {1: 1, 2: 2, 3: 3}

    列表追加

    >>> list1=[]
    >>> list1.append("a")
    >>> list1
    ['a']
    >>> list1.append("b")
    >>> list1
    ['a', 'b']

    >>> list1.extend("c")
    >>> list1
    ['a', 'b', 'c']
    >>> list1.extend(["d","e","f"])
    >>> list1
    ['a', 'b', 'c', 'd', 'e', 'f']

  • 相关阅读:
    PHP获取今天、昨天、明天的日期
    PHP使用Apache中的ab测试网站的压力性能
    php 数组操作
    ajax实时获取下拉数据
    php微信支付测试开发(流程已通)
    微信支付调用JSAPI缺少参数:timeStamp
    JS dom最常用API
    Javascript刷新页面的几种方法
    php+mysql 安全
    隐式的类类型转换
  • 原文地址:https://www.cnblogs.com/fanxuanhui-linux/p/5876414.html
Copyright © 2011-2022 走看看