The list data type has some more methods. Here are all of the methods of list objects:
链表类型有很多方法,这里是链表类型的所有方法:
x) |
a[len(a):] = [x]
.
把一个元素添加到链表的结尾,相当于 a[len(a):] = [x]
L) |
通过添加指定链表的所有元素来扩充链表,相当于 a[len(a):] = L
。
i, x) |
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)
。
x) |
删除链表中值为x的第一个元素。如果没有这样的元素,就会返回一个错误。
[i]) |
a.pop()
returns the last item in the
list. The item is also removed from 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 库参考手册中遇到这样的标记。)
x) |
返回链表中第一个值为x的元素的索引。如果没有匹配的元素就会返回一个错误。
x) |
返回x在链表中出现的次数。
) |
对链表中的元素进行适当的排序。
) |
倒排链表中的元素。
An example that uses most of the list methods:
下面这个示例演示了链表的大部分方法:
>>> a = [66.6, 333, 333, 1, 1234.5] >>> print a.count(333), a.count(66.6), a.count('x') 2 1 0 >>> a.insert(2, -1) >>> a.append(333) >>> a [66.6, 333, -1, 333, 1, 1234.5, 333] >>> a.index(333) 1 >>> a.remove(333) >>> a [66.6, -1, 333, 1, 1234.5, 333] >>> a.reverse() >>> a [333, 1234.5, 1, 333, -1, 66.6] >>> a.sort() >>> a [-1, 1, 66.6, 333, 333, 1234.5]