@nalan90
2017-07-28T09:30:48.000000Z
字数 4286
阅读 761
Python高效编程技巧实战
常用函数
list.append(x)
Add an item to the end of the list; equivalent to a[len(a):] = [x].
In [2]: l = range(10)In [3]: lOut[3]: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]In [4]: l.append('20')In [5]: lOut[5]: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, '20']In [6]: l.append('30')
list.extend(L)
Extend the list by appending all the items in the given list; equivalent to a[len(a):] = L.
In [7]: l2 = [100,200,300,400]In [8]: l.extend(l2)In [9]: print l[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, '20', '30', 100, 200, 300, 400]
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).
In [10]: l.insert(0,100)In [11]: print l[100, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, '20', '30', 100, 200, 300, 400]In [12]: l.insert(5,2000)In [13]: print l[100, 0, 1, 2, 3, 2000, 4, 5, 6, 7, 8, 9, '20', '30', 100, 200, 300, 400]
list.remove(x)
Remove the first item from the list whose value is x. It is an error if there is no such item.
In [14]: l.remove(100)In [15]: print l[0, 1, 2, 3, 2000, 4, 5, 6, 7, 8, 9, '20', '30', 100, 200, 300, 400]## 若值不存在,直接报错In [16]: l.remove(10000)---------------------------------------------------------------------------ValueError Traceback (most recent call last)<ipython-input-16-869a30041ac7> in <module>()----> 1 l.remove(10000)ValueError: list.remove(x): x not in list
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.)
In [17]: l.pop()Out[17]: 400In [18]: print l[0, 1, 2, 3, 2000, 4, 5, 6, 7, 8, 9, '20', '30', 100, 200, 300]## 若列表下标索引不存在,直接报错In [19]: l.pop(30)---------------------------------------------------------------------------IndexError Traceback (most recent call last)<ipython-input-19-bc5fb6cfa67f> in <module>()----> 1 l.pop(30)IndexError: pop index out of range
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.
## 列表值不存在,直接报错In [20]: l.index(20)---------------------------------------------------------------------------ValueError Traceback (most recent call last)<ipython-input-20-c2ebae669f21> in <module>()----> 1 l.index(20)ValueError: 20 is not in listIn [21]: l.index(4)Out[21]: 5
list.count(x)
Return the number of times x appears in the list.
In [22]: print l[0, 1, 2, 3, 2000, 4, 5, 6, 7, 8, 9, '20', '30', 100, 200, 300]In [23]: l.count(4)Out[23]: 1In [24]: l.count(23)Out[24]: 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).
In [25]: lOut[25]: [0, 1, 2, 3, 2000, 4, 5, 6, 7, 8, 9, '20', '30', 100, 200, 300]In [26]: l.sort()In [27]: print l[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 100, 200, 300, 2000, '20', '30']In [28]: l.sort(reverse=True)In [29]: print l['30', '20', 2000, 300, 200, 100, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
list.reverse()
Reverse the elements of the list, in place.
In [30]: l.reverse()In [31]: print l[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 100, 200, 300, 2000, '20', '30']
注意事项
## 列表赋值In [44]: lOut[44]: [3, 2, 3, 4, 5, 6, 7, 8, 9, 10]In [45]: l = range(1,11)In [46]: lOut[46]: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]In [47]: l2 = l## 列表直接赋值,两者的内存地址一致In [61]: id(l),id(l2)Out[61]: (4412312464, 4412312464)In [48]: l2Out[48]: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]In [49]: l2[3] = 100## l的元素也发生变化In [50]: lOut[50]: [1, 2, 3, 100, 5, 6, 7, 8, 9, 10]
## copy(浅拷贝)In [52]: import copyIn [53]: l2 = copy.copy(l)## 使用copy生成的列表,内存地址发生变化In [58]: id(l),id(l2)Out[58]: (4412312464, 4413391000)In [54]: l2Out[54]: [1, 2, 3, 100, 5, 6, 7, 8, 9, 10]In [55]: lOut[55]: [1, 2, 3, 100, 5, 6, 7, 8, 9, 10]## 更改l2的元素,不会传播至lIn [56]: l2[3] = 200In [57]: lOut[57]: [1, 2, 3, 100, 5, 6, 7, 8, 9, 10]
## deepcopy(深拷贝)In [63]: lOut[63]: [1, 2, 3, 4, 5, [6, 7], 8, 9]In [64]: l2 = copy.copy(l)In [65]: l2Out[65]: [1, 2, 3, 4, 5, [6, 7], 8, 9]In [67]: l2[5][0] = 100In [68]: l2Out[68]: [1, 2, 3, 4, 5, [100, 7], 8, 9]In [69]: lOut[69]: [1, 2, 3, 4, 5, [100, 7], 8, 9]In [70]: id(l),id(l2)Out[70]: (4413441664, 4413393520)## copy对于子列表引用的还是同一个内存地址In [71]: id(l[5])Out[71]: 4413443824In [72]: id(l2[5])Out[72]: 4413443824## 使用deepcopyIn [75]: l2 = copy.deepcopy(l)In [76]: l2Out[76]: [1, 2, 3, 4, 5, [100, 7], 8, 9]In [77]: id(l),id(l2)Out[77]: (4413441664, 4413394168)In [78]: id(l[5]),id(l2[5])Out[78]: (4413443824, 4412172480)In [79]: l2[5][0] = 200In [80]: l2Out[80]: [1, 2, 3, 4, 5, [200, 7], 8, 9]In [81]: lOut[81]: [1, 2, 3, 4, 5, [100, 7], 8, 9]