More on Lists in Python----深入Python列表

    The list data type has some more methods. Here are all of the methods of list objects:
    链表类型有很多方法,这里是链表类型的所有方法:

list.append(x)

    Add an item to the end of the list; equivalent to a[len(a):] = [x].

    把一个元素添加到链表的结尾,相当于 a[len(a):] = [x] 。


list.extend(L)

    Extend the list by appending all the items in the given list; equivalent to a[len(a):] =L.

    将一个给定列表中的所有元素都添加到另一个列表中,相当于 a[len(a):] = L 。


list.insert(i, x)

    Insert an item at a given position. The first argument is the index of the element before which to insert, so a.insert(0, x) inserts at the front of the list, and a.insert(len(a),x) is equivalent to a.append(x).

    在指定位置插入一个元素。第一个参数是准备插入到其前面的那个元素的索引,例如a.insert(0,x) 会插入到整个链表之前,而 a.insert(len(a), x) 相当于 a.append(x) 。


list.remove(x)

    Remove the first item from the list whose value is x. It is an error if there is no such item.

    删除链表中值为 x 的第一个元素。如果没有这样的元素,就会返回一个错误。


list.pop( [ i ] )

    Remove the item at the given position in the list, and return it. If no index is specified, a.pop() removes and returns the last item in the list. (The square brackets around the i in the method signature denote that the parameter is optional, not that you should type square brackets at that position. You will see this notation frequently in the Python Library Reference.)

    从链表的指定位置删除元素,并将其返回。如果没有指定索引, a.pop() 返 回最后一个元素。元素随即从链表中被删除。(方法中 i 两边的方括号表示 这个参数是可选的,而不是要求你输入一对方括号,你会经常在Python 库参 考手册中遇到这样的标记。)


list.index(x)

    Return the index in the list of the first item whose value is x. It is an error if there
is no such item.

    返回链表中第一个值为 x 的元素的索引。如果没有匹配的元素就会返回一个错误。


list.count(x)

    Return the number of times x appears in the list. 

    返回 x 在链表中出现的次数。


list.sort()

    Sort the items of the list, in place.

    对链表中的元素就地(原文 in place,意即该操作直接修改调用它的对象——译者)进行排序。


list.reverse()

    Reverse the elements of the list, in place.

    就地倒排链表中的元素。


An example that uses most of the list methods:
下面这个示例演示了链表的大部分方法


>>> a = [66.25, 333, 333, 1, 1234.5]
>>> print a.count(333), a.count(66.25), a.count('x')
2 1 0
>>> a.insert(2, -1)
>>> a.append(333)
>>> a
[66.25, 333, -1, 333, 1, 1234.5, 333]
>>> a.index(333)
1
>>> a.remove(333)
>>> a
[66.25, -1, 333, 1, 1234.5, 333]
>>> a.reverse()
>>> a
[333, 1234.5, 1, 333, -1, 66.25]
>>> a.sort()
>>> a
[-1, 1, 66.25, 333, 333, 1234.5]


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值