函数名:append
#将单个项添加到现有列表中。它不会返回一个新的列表;相反,它修改了原始列表。
>>> a=[1]
>>> a.append(2)
>>> a
[1, 2]
L.append(object) -> None -- appendobject to end
函数名:clear
#clear()方法从列表中删除所有项。
>>> b
[1, 2, 3, 1, 5, 6]
>>> b.clear()
>>> b
[]
L.clear() -> None -- remove all itemsfrom L
函数名:copy
#返回列表的一个浅拷贝。
>>> a=[1,2,3,1,5,6]
>>> b=a.copy()
>>> a=[1,2,3,1,5,6]
>>> b=a.copy()
L.copy() -> list -- a shallow copy of L
函数名:count
#count()方法返回列表中元素的出现次数。
>>> a=[1,2,3,1,5,6]
>>> a.count(1)
2
L.count(value) -> integer -- return numberof occurrences of value
函数名:extend
#扩展列表,将列表的所有项(作为参数传递)添加到末尾。
>>> language = ['French', 'English', 'German']
>>> language1 = ['Spanish', 'Portuguese']
>>> language.extend(language1)
>>> language
['French', 'English', 'German', 'Spanish', 'Portuguese']
L.extend(iterable) -> None -- extendlist by appending elements from the iterable
函数名:index
#index()方法在列表中搜索一个元素并返回它的索引。
#索引方法采用单个参数:元素——要搜索的元素。不存在会报错
>>> a=[1,2,3,4,5,6]
>>> a.index(3)
2
>>> a.index(7)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: 7 is not in list
L.index(value, [start, [stop]]) ->integer -- return first index of value.
Raises ValueError if the value is notpresent.
函数名:insert
#insert()方法将元素插入到给定索引的列表中。
insert()函数包含两个参数:
索引——需要插入元素的位置。
元素——这是要插入到列表中的元素。
>>> a=[1,2,3,4,5,6]
>>> a.insert(1,'a')
>>> a
[1, 'a', 2, 3, 4, 5, 6]
L.insert(index, object) -- insert objectbefore index
函数名:pop
#pop()方法从列表中删除并返回给定索引中的元素(作为参数传递)。
pop()方法采用单个参数(index),并从列表中删除该索引中的元素。
如果传递给pop()方法的索引不在范围内,它会抛出IndexError: pop索引超出范围异常。
传递给pop()方法的参数是可选的。如果没有传递参数,则将默认索引-1作为参数传递,以返回最后一个元素。
>>> a=[1,2,3,1,5,6]
>>> a.pop(3)
1
>>> a.pop()
6
>>> a
[1, 2, 3, 5]
L.pop([index]) -> item -- remove andreturn item at index (default last).
Raises
L.sort(key=None, reverse=False) -> None-- stable sort *IN PLACE*
IndexError if list is empty or indexis out of range.
函数名:remove
#remove()方法搜索列表中的给定元素并删除第一个匹配元素。
remove()方法将单个元素作为参数,并将其从列表中删除。
如果传递给remove()方法的元素(参数)不存在,则抛出valueError异常。
>>> a=[1,2,3,4,5,6]
>>> a.remove(2)
>>> a
[1, 3, 4, 5, 6]
>>> a.remove(7)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: list.remove(x): x not in list
L.remove(value) -> None -- remove firstoccurrence of value.
Raises ValueError if the value is notpresent.
函数名:reverse
#反转给定列表的元素。
#函数的作用是:不返回任何值。它只返回元素并更新列表。
>>> a=[1,2,3,1,5,6]
>>> a.reverse()
>>> a
[6, 5, 1, 3, 2, 1]
L.reverse() -- reverse *IN PLACE*
函数名:sort
#对给定列表的元素进行排序。
默认情况下,sort()不需要任何额外的参数。但是,它有两个可选参数:
reverse——默认为False,排序列表将被反向(或按降序排序)
key-函数,作为排序比较的参数。
def takeSecond(elem):
return elem[1]
random = [(2, 2), (3, 4), (4, 1), (1, 3)]
random.sort(key=takeSecond)
print('Sorted list:', random)
Sorted list: [(4, 1), (2, 2), (1, 3), (3, 4)]
L.sort(key=None, reverse=False) -> None-- stable sort *IN PLACE*