list.reverse 反转列表中元素
列表方法 list.reverse(),Python 官方文档描述如下:
help(list.reverse)
Help on method_descriptor:
reverse(self, /)
Reverse *IN PLACE*.
反转列表中的元素。
_list = [1,2,3]
_list.reverse()
_list
[3, 2, 1]
该方法是一个过程 (过程就是不返回有意义结果的函数;在 Python 中,过程的返回值为 None), 直接对原列表进行修改:
_list = [1,2,3]
a = _list.reverse()
print(a)
None
list.pop 删除元素并返回
列表方法 list.pop(),Python 官方文档描述如下:
help(list.pop)
Help on method_descriptor:
pop(self, index=-1, /)
Remove and return item at index (default last).
Raises IndexError if list is empty or index is out of range.
删除列表中给定位置的元素并返回它。如果没有给定位置,list.pop() 将会删除并返回列表中的最后一个元素。给定位置超出范围,抛出 IndexError 错误。
_list = [1,2,3,4]
_list.pop()
4
_list
[1, 2, 3]
_list.pop(0)
1
_list
[2, 3]
_list.pop(5)
---------------------------------------------------------------------------
IndexError Traceback (most recent call last)
<ipython-input-4-efa5a84417c8> in <module>
----> 1 _list.pop(5)
IndexError: pop index out of range
list.remove 移除一个元素
列表方法 list.remove(),Python 官方文档描述如下:
help(list.remove)
Help on method_descriptor:
remove(self, value, /)
Remove first occurrence of value.
Raises ValueError if the value is not present.
移除列表中第一个值为 value 的元素。如果没有这样的元素,则抛出 ValueError 异常。
_list = [1,3,2,3]
_list.remove(3)
_list
[1, 2, 3]
_list.remove(4)
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-3-5747ddae04fe> in <module>
----> 1 _list.remove(4)
ValueError: list.remove(x): x not in list
该方法是一个过程 (过程就是不返回有意义结果的函数;在 Python 中,过程的返回值为 None), 直接对原列表进行修改:
_list = [1,3,2,3]
a = _list.remove(3)
print(a)
None
list.count 统计元素出现次数
列表方法 list.count(),Python 官方文档描述如下:
help(list.count)
Help on method_descriptor:
count(self, value, /)
Return number of occurrences of value.
返回元素 value 在列表中出现的次数,没有出现为 0。
_list = [1,2,3,1]
_list.count(1), _list.count([1])
(2, 0)
list.index 查找最小索引
列表方法 list.index(),Python 官方文档描述如下:
help(list.index)
Help on method_descriptor:
index(self, value, start=0, stop=9223372036854775807, /)
Return first index of value.
Raises ValueError if the value is not present.
返回列表中第一个值为 value 的元素从零开始的索引。如果没有这样的元素将会抛出 ValueError 异常。
可选参数 start 和 stop 是切片符号,用于将搜索限制为列表的特定子序列。返回的是相对于整个序列开始计算的索引,而不是相对于 start 参数。
_list = [1,2,3,4,3]
_list.index(3)
2
_list.index(3,1,4)
2
_list.index(3,4,10)
4
_list.index(3,5,10)
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-6-3e258cbffc85> in <module>
----> 1 _list.index(3,5,10)
ValueError: 3 is not in list
list.copy 列表的一个浅拷贝
列表方法 list.copy(),Python 官方文档描述如下:
help(list.copy)
Help on method_descriptor:
copy(self, /)
Return a shallow copy of the list.
返回列表的一个浅拷贝。相当于 a[:](a 是一个列表)。浅拷贝得到新的列表,列表中有可变对象时,浅拷贝中的可变对象元素,是原列表中同一个对象的多次引用。
list_1 = [[1],2,3]
list_2 = list_1.copy()
print(list_2)
id(list_1),id(list_2)
[[1], 2, 3]
(2622792783240, 2622792782728)
# 同一个对象
id(list_1[0]), id(list_2[0])
(2622792782792, 2622792782792)
改变其中一个都会跟着改变:
list_1[0][:] = 'abc'
list_1, list_2
([['a', 'b', 'c'], 2, 3], [['a', 'b', 'c'], 2, 3])
list.clear 删除所有元素
列表方法 list.clear(),Python 官方文档描述如下:
help(list.clear)
Help on method_descriptor:
clear(self, /)
Remove all items from list.
删除列表中所有的元素。相当于 del a[:](a 是列表)。
_list = [1,2,3]
_list.clear()
_list
[]
del a[:] 是删除列表 a 中的所有元素,和 a 的浅拷贝无关:
a = [1,2,3]
b = a[:]
del a[:]
a, b
([], [1, 2, 3])
该方法是一个过程 (过程就是不返回有意义结果的函数;在 Python 中,过程的返回值为 None), 直接对原列表进行修改:
_list = [1,2,3]
a = _list.clear()
print(a)
None