• python常用类型的内置函数列表


            1、list.append(obj)         向列表中加入一个对象obj

     fruits = ['apple', 'pear', 'orange']
    >>> fruits.append('apple')
    >>> fruits
    ['apple', 'pear', 'orange', 'apple']
    

     

            2、list.count(obj)             返回一个对象obj在列表中出现的次数

    >>> fruits.count('apple')
    2
    

     

            3、list.extend(seq)          把序列seq的内容加入到列表中

    >>> seq = ['banana', 'strawberry']
    >>> fruits.extend(seq)
    >>> fruits
    ['apple', 'pear', 'orange', 'apple', 'banana', 'strawberry']
    

            4、list.index(obj, i=0, j=len(list)) 

            返回 list[k] == obj 的 k 值,而且 k 的范围在 i<=k<j;否则引发 ValueError 异常。

    >>> fruits.index('orange',0, len(list))
    2
    >>> fruits.index('lemon',0, len(list))
    
    Traceback (most recent call last):
      File "<pyshell#11>", line 1, in <module>
        fruits.index('lemon',0, len(list))
    ValueError: 'lemon' is not in list
    

            5、list.insert(index, obj)             在索引量为 index 的位置插入对象obj

    >>> fruits.insert(3, 'lemon')
    >>> fruits
    ['apple', 'pear', 'orange', 'lemon', 'apple', 'banana', 'strawberry']
    

     

            6、list.pop(index=-1)                  删除并返回指定位置的对象,默认是最后一个对象

    >>> fruits.pop()
    'strawberry'
    >>> fruits
    ['apple', 'pear', 'orange', 'lemon', 'apple', 'banana']
    

            7、list.remove(obj)                     从列表中删除找到的第一个obj对象。假设不存在则返回一个ValueError错误。

    >>> fruits.remove('apple') >>> fruits ['pear', 'orange', 'lemon', 'apple', 'banana'] >>> fruits.remove('strawberry')

    Traceback (most recent call last):   File "<pyshell#25>", line 1, in <module>     fruits.remove('strawberry') ValueError: list.remove(x): x not in list

           8、list.reverse()             原地翻转列表

    >>> fruits.reverse()
    >>> fruits
    ['banana', 'apple', 'lemon', 'orange', 'pear']
    

            9、list.sort(func=None,key=None, reverse=False)
            以指定的方式排序列表中的成员,假设 func 和 key 參数指定,则依照指定的方式比較各个元素,假设 reverse 标志被置为True,则列表以反序排列。
       

    >>> fruits.sort()
    >>> fruits
    ['apple', 'banana', 'lemon', 'orange', 'pear']
    >>> fruits.sort(reverse=True)
    >>> fruits
    ['pear', 'orange', 'lemon', 'banana', 'apple']
    


     

    版权声明:本文博主原创文章。博客,未经同意不得转载。

  • 相关阅读:
    sencha touch 入门学习资料大全
    细说websocket
    【读fastclick源码有感】彻底解决tap“点透”,提升移动端点击响应速度
    新鲜的前端效果,边栏菜单、滑动效果
    PhoneGap+JQuery Mobile移动应用开发学习笔记
    21个值得收藏的Javascript技巧
    NodeJS无所不能:细数10个令人惊讶的NodeJS开源项目
    Node.js 中文学习资料和教程导航
    PayPal为什么从Java迁移到Node.js 性能提高一倍 文件代码减少44%
    知道创宇研发技能表v2.1
  • 原文地址:https://www.cnblogs.com/zfyouxi/p/4889425.html
Copyright © 2020-2023  润新知