[关闭]
@nalan90 2017-07-28T17:30:48.000000Z 字数 4286 阅读 591

专题一 list常用操作

Python高效编程技巧实战


常用函数

list.append(x)

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

  1. In [2]: l = range(10)
  2. In [3]: l
  3. Out[3]: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
  4. In [4]: l.append('20')
  5. In [5]: l
  6. Out[5]: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, '20']
  7. 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.

  1. In [7]: l2 = [100,200,300,400]
  2. In [8]: l.extend(l2)
  3. In [9]: print l
  4. [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).

  1. In [10]: l.insert(0,100)
  2. In [11]: print l
  3. [100, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, '20', '30', 100, 200, 300, 400]
  4. In [12]: l.insert(5,2000)
  5. In [13]: print l
  6. [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.

  1. In [14]: l.remove(100)
  2. In [15]: print l
  3. [0, 1, 2, 3, 2000, 4, 5, 6, 7, 8, 9, '20', '30', 100, 200, 300, 400]
  4. ## 若值不存在,直接报错
  5. In [16]: l.remove(10000)
  6. ---------------------------------------------------------------------------
  7. ValueError Traceback (most recent call last)
  8. <ipython-input-16-869a30041ac7> in <module>()
  9. ----> 1 l.remove(10000)
  10. 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.)

  1. In [17]: l.pop()
  2. Out[17]: 400
  3. In [18]: print l
  4. [0, 1, 2, 3, 2000, 4, 5, 6, 7, 8, 9, '20', '30', 100, 200, 300]
  5. ## 若列表下标索引不存在,直接报错
  6. In [19]: l.pop(30)
  7. ---------------------------------------------------------------------------
  8. IndexError Traceback (most recent call last)
  9. <ipython-input-19-bc5fb6cfa67f> in <module>()
  10. ----> 1 l.pop(30)
  11. 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.

  1. ## 列表值不存在,直接报错
  2. In [20]: l.index(20)
  3. ---------------------------------------------------------------------------
  4. ValueError Traceback (most recent call last)
  5. <ipython-input-20-c2ebae669f21> in <module>()
  6. ----> 1 l.index(20)
  7. ValueError: 20 is not in list
  8. In [21]: l.index(4)
  9. Out[21]: 5

list.count(x)

Return the number of times x appears in the list.

  1. In [22]: print l
  2. [0, 1, 2, 3, 2000, 4, 5, 6, 7, 8, 9, '20', '30', 100, 200, 300]
  3. In [23]: l.count(4)
  4. Out[23]: 1
  5. In [24]: l.count(23)
  6. 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).

  1. In [25]: l
  2. Out[25]: [0, 1, 2, 3, 2000, 4, 5, 6, 7, 8, 9, '20', '30', 100, 200, 300]
  3. In [26]: l.sort()
  4. In [27]: print l
  5. [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 100, 200, 300, 2000, '20', '30']
  6. In [28]: l.sort(reverse=True)
  7. In [29]: print l
  8. ['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.

  1. In [30]: l.reverse()
  2. In [31]: print l
  3. [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 100, 200, 300, 2000, '20', '30']

注意事项

  1. ## 列表赋值
  2. In [44]: l
  3. Out[44]: [3, 2, 3, 4, 5, 6, 7, 8, 9, 10]
  4. In [45]: l = range(1,11)
  5. In [46]: l
  6. Out[46]: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
  7. In [47]: l2 = l
  8. ## 列表直接赋值,两者的内存地址一致
  9. In [61]: id(l),id(l2)
  10. Out[61]: (4412312464, 4412312464)
  11. In [48]: l2
  12. Out[48]: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
  13. In [49]: l2[3] = 100
  14. ## l的元素也发生变化
  15. In [50]: l
  16. Out[50]: [1, 2, 3, 100, 5, 6, 7, 8, 9, 10]

  1. ## copy(浅拷贝)
  2. In [52]: import copy
  3. In [53]: l2 = copy.copy(l)
  4. ## 使用copy生成的列表,内存地址发生变化
  5. In [58]: id(l),id(l2)
  6. Out[58]: (4412312464, 4413391000)
  7. In [54]: l2
  8. Out[54]: [1, 2, 3, 100, 5, 6, 7, 8, 9, 10]
  9. In [55]: l
  10. Out[55]: [1, 2, 3, 100, 5, 6, 7, 8, 9, 10]
  11. ## 更改l2的元素,不会传播至l
  12. In [56]: l2[3] = 200
  13. In [57]: l
  14. Out[57]: [1, 2, 3, 100, 5, 6, 7, 8, 9, 10]

  1. ## deepcopy(深拷贝)
  2. In [63]: l
  3. Out[63]: [1, 2, 3, 4, 5, [6, 7], 8, 9]
  4. In [64]: l2 = copy.copy(l)
  5. In [65]: l2
  6. Out[65]: [1, 2, 3, 4, 5, [6, 7], 8, 9]
  7. In [67]: l2[5][0] = 100
  8. In [68]: l2
  9. Out[68]: [1, 2, 3, 4, 5, [100, 7], 8, 9]
  10. In [69]: l
  11. Out[69]: [1, 2, 3, 4, 5, [100, 7], 8, 9]
  12. In [70]: id(l),id(l2)
  13. Out[70]: (4413441664, 4413393520)
  14. ## copy对于子列表引用的还是同一个内存地址
  15. In [71]: id(l[5])
  16. Out[71]: 4413443824
  17. In [72]: id(l2[5])
  18. Out[72]: 4413443824
  19. ## 使用deepcopy
  20. In [75]: l2 = copy.deepcopy(l)
  21. In [76]: l2
  22. Out[76]: [1, 2, 3, 4, 5, [100, 7], 8, 9]
  23. In [77]: id(l),id(l2)
  24. Out[77]: (4413441664, 4413394168)
  25. In [78]: id(l[5]),id(l2[5])
  26. Out[78]: (4413443824, 4412172480)
  27. In [79]: l2[5][0] = 200
  28. In [80]: l2
  29. Out[80]: [1, 2, 3, 4, 5, [200, 7], 8, 9]
  30. In [81]: l
  31. Out[81]: [1, 2, 3, 4, 5, [100, 7], 8, 9]
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注