• python中List操作


    传送门 官方文件地址

    list.append(x):

           将x加入列表尾部,等价于a[len(a):] = [x]

           例:

    >>> list1=[1,2,3,4]
    >>> list1.append(5)
    >>> list1
    [1, 2, 3, 4, 5]

    list.extend(L)

    将列表L中的元素加入list中,等价于a[len(a):] = L.

    例:

    >>> list1=[1,2,3,4]
    >>> L=[5,6,7,8]
    >>> list1.extend(L)
    >>> list1
    [1, 2, 3, 4, 5, 6, 7, 8]

    list.insert(i, x)

    在指定位置插入元素。第一个参数指定哪一个位置前插入元素。a.insert(0,x)就是在列表最前方插入,a.insert(len(a),x)则等价于a.append(x)

    例:

    >>>list1=[1,2,3,4]
    >>> list1.insert(1,45)
    >>> list1
    [1, 45, 2, 3, 4]

    list.remove(x)

    移除list中值为x的第一个元素,如果没有这样的元素,则返回error,
    例:

    >>> list1=[1,2,3,4,5,1,2,3,4,5]
    >>> list1.remove(1,2)
    Traceback (most recent call last):
      File "<pyshell#80>", line 1, in <module>
        list1.remove(1,2)
    TypeError: remove() takes exactly one argument (2 given)
    >>> list1.remove(1)
    >>> list1
    [2, 3, 4, 5, 1, 2, 3, 4, 5]

    list.pop([i])

    list.pop()删除列表中的最后一个值并返回该值

    list.pop(n)返回列表中的第n+1个值并删除

    返会最后一个元素并从列表中删除:

    例:

    >>> list1=[0,1,2,3,4,5]
    >>> list1.pop(3)
    3
    >>> list1
    [0, 1, 2, 4, 5]
    >>> list1.pop() 5 >>> list2=[1] >>> list2.pop() 1 >>> list2 []

    list.index(x)

    返回列表中第一个值为x的位置,如果没有这样的元素则返回错误

    例:

    >>> list1=[1,2,3,4,5]
    >>> list1.index(3)
    2
    >>> list1.index(6)
    
    Traceback (most recent call last):
      File "<pyshell#130>", line 1, in <module>
        list1.index(6)
    ValueError: list.index(x): x not in list

    list.count(x)

    返回x在列表中出现的次数

    例:

    >>> list1=[1,2,3,4,5,6,1,3,4,5,1,4,5]
    >>> list1.count(1)
    3
    >>> list1.count(9)
    0

    list.sort(cmp=None, key=None, reverse=False)

    Sort the items of the list in place (the arguments can be used for sort customization,

    see sorted() for their explanation).

    list.reverse()

    翻转该列表

    例:

    >>> list1=[1,2,3,4,5]
    >>> list1.reverse()
    >>> list1
    [5, 4, 3, 2, 1]
  • 相关阅读:
    MEF(Managed Extensibility Framework ) 可控扩展框架
    如何打开ASP.NET Configuration页面
    [转贴]技术的乐趣
    ORM工具介绍 NHibernate, EntitySpaces, and LLBLGen Pro 功能分析
    Linq to SQL 学习路线图
    [转贴]What is AntiPattern 什么是反模式
    Master Data Management(MDM)主数据管理
    Introducing Unity Application Block
    C#2.0 C#3.0 语言特性
    javascript声明数组三种方式
  • 原文地址:https://www.cnblogs.com/fei-hsueh/p/3977467.html
Copyright © 2020-2023  润新知