• 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']

  • 相关阅读:
    Flask基础
    Scrapy框架(持久化,去重,深度控制,cookie)
    scrapy框架 简易整理
    BeautifulSoup 模块
    requests模块
    复习第三天
    在Django中使用原生Sql
    ajax跨域简单请求和复杂请求
    复习第二天
    IOS
  • 原文地址:https://www.cnblogs.com/fanxuanhui-linux/p/5876414.html
Copyright © 2020-2023  润新知