1、list.append() 想列表中添加一个元素,在列表的最后进行追加
In [28]: help(l1.append)
Help on built-in function append:
append(...) method of builtins.list instance
L.append(object) -> None -- append object to end
In [25]: l1
Out[25]: [1, 2, 3, 4]
In [26]: l1.append(5)
In [27]: l1
Out[27]: [1, 2, 3, 4, 5]
2、list.copy() 列表的复制,将原列表复制给一个新的列表
In [36]: help(l1.copy)
Help on built-in function copy:
copy(...) method of builtins.list instance
L.copy() -> list -- a shallow copy of L
In [32]: l1
Out[32]: [1, 2, 3, 4, 5]
In [33]: l2
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-33-ea320d2ace30> in <module>()
----> 1 l2
NameError: name 'l2' is not defined
In [34]: l2 = l1.copy()
In [35]: l2
Out[35]: [1, 2, 3, 4, 5]
list.clear() 清空列表中所有的元素,列表变成一个空的列表
In [37]: l2.clear()
In [38]: l2
Out[38]: []
list.count() 统计列表中某个元素有多少个
In [40]: l1 = [1,3,2,5,2,1,3,1,3,2,1]
In [41]: l1.count(1)
Out[41]: 4
list.extend(iterable)
In [42]: help(list.count)
Help on method_descriptor:
count(...)
L.count(value) -> integer -- return number of occurrences of value
In [43]: help(l1.extend)
Help on built-in function extend:
extend(...) method of builtins.list instance
L.extend(iterable) -> None -- extend list by appending elements from the iterable
In [45]: l1 = [1,2,3,4]
In [46]: l2 = [5,6,7,8]
In [47]: l1.extend(l2)
In [48]: l1
Out[48]: [1, 2, 3, 4, 5, 6, 7, 8]
In [49]: l1
Out[49]: [1, 2, 3, 4, 5, 6, 7, 8]
In [50]: l1.extend('abcd')
In [51]: l1
Out[51]: [1, 2, 3, 4, 5, 6, 7, 8, 'a', 'b', 'c', 'd']
In [52]: l1.extend((9,10))
In [53]: l1
Out[53]: [1, 2, 3, 4, 5, 6, 7, 8, 'a', 'b', 'c', 'd', 9, 10]
In [54]: s1 = set((11,22))
In [55]: type(s1)
Out[55]: set
In [56]: l1.extend(s1)
In [57]: l1
Out[57]: [1, 2, 3, 4, 5, 6, 7, 8, 'a', 'b', 'c', 'd', 9, 10, 11, 22]
In [58]: d1 = {"key1":1,"key2":2}
In [59]: l1.extend(d1)
# 字典将key加入进去
In [60]: l1
Out[60]: [1, 2, 3, 4, 5, 6, 7, 8, 'a', 'b', 'c', 'd', 9, 10, 11, 22, 'key1', 'key2']
list.index() 求元素的索引值
In [65]: help(list.index)
Help on method_descriptor:
index(...)
L.index(value, [start, [stop]]) -> integer -- return first index of value.
Raises ValueError if the value is not present.
(END)
# 注意 如果存在多个相同的元素,index 返回最小值
In [62]: l1 = [1,3,2,1,4,2,1,3]
In [63]: l1.index(1)
Out[63]: 0
In [64]: l1.index(2)
Out[64]: 2
In [66]: l1.index(1,2,4)
Out[66]: 3
# start <= index < stop 和list切片一样,算头不算尾的半闭包
In [67]: l1.index(2,3,5)
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-67-f936af80f256> in <module>()
----> 1 l1.index(2,3,5)
ValueError: 2 is not in list
In [68]: l1.index(2,3,6)
Out[68]: 5
list.insert() 向列表指定的位置插入一个元素,和append的区别,append只能在列表的尾部追加,而insert 可以自行指定插入的位置
In [69]: help(l1.insert)
Help on built-in function insert:
insert(...) method of builtins.list instance
L.insert(index, object) -- insert object before index
In [75]: l1
Out[75]: [1, 3, 2, 1, 10, 4, 2, 1, 3]
In [76]: l1.index(10)
Out[76]: 4
In [77]: l1.insert(4,'abc')
In [78]: l1
Out[78]: [1, 3, 2, 1, 'abc', 10, 4, 2, 1, 3]
In [79]: l1.index(10)
Out[79]: 5
In [80]: l1.index('abc')
Out[80]: 4
# insert() 函数有两个参数
In [81]: l1.insert('cde')
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-81-e9cb4bf3524f> in <module>()
----> 1 l1.insert('cde')
TypeError: insert() takes exactly 2 arguments (1 given)
list.pop() 列表中删除一个元素,默认情况下删除最后一个
In [86]: help(l1.pop)
Help on built-in function pop:
pop(...) method of builtins.list instance
L.pop([index]) -> item -- remove and return item at index (default last).
Raises IndexError if list is empty or index is out of range.
In [89]: l1
Out[89]: [1, 3, 2, 1, 'abc', 10, 4, 2, 1, 3]
In [90]: l1.pop()
Out[90]: 3
In [91]: l1.index('abc')
Out[91]: 4
In [92]: l1.pop(4)
Out[92]: 'abc'
In [93]: l1
Out[93]: [1, 3, 2, 1, 10, 4, 2, 1]
list.remove() 删除某个元素,直接给元素的值,而不是索引,如果该元素有多个,删除索引值小的那个
In [94]: help(l1.remove)
Help on built-in function remove:
remove(...) method of builtins.list instance
L.remove(value) -> None -- remove first occurrence of value.
Raises ValueError if the value is not present.
# 删除第1个1
In [95]: l1
Out[95]: [1, 3, 2, 1, 10, 4, 2, 1]
In [97]: l1.index(1)
Out[97]: 0
In [98]: l1.remove(1)
In [99]: l1
Out[99]: [3, 2, 1, 10, 4, 2, 1]
In [100]: l1.remove()
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-100-b29bbf1c2737> in <module>()
----> 1 l1.remove()
TypeError: remove() takes exactly one argument (0 given)
In [101]: l1.remove(5)
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-101-93b2385f14d6> in <module>()
----> 1 l1.remove(5)
ValueError: list.remove(x): x not in list
list.reverse() 在原地将列表中所有的元素按逆序存储,列表发生变化
In [103]: l1
Out[103]: [3, 2, 1, 10, 4, 2, 1]
In [104]: l1.reverse()
In [105]: l1
Out[105]: [1, 2, 4, 10, 1, 2, 3]
list.sort() 列表自带的排序函数,从小到大升序排序,这个性能上比sorted(l1) 要差这个不改变列表的值,返回排好序的列表
In [106]: l1.sort()
In [107]: l1
Out[107]: [1, 1, 2, 2, 3, 4, 10]
In [109]: l1 = [3,1,5,10,2,31,21,9]
In [110]: sorted(l1)
Out[110]: [1, 2, 3, 5, 9, 10, 21, 31]
In [111]: l1
Out[111]: [3, 1, 5, 10, 2, 31, 21, 9]
In [112]: l1.sort()
In [113]: l1
Out[113]: [1, 2, 3, 5, 9, 10, 21, 31]
这里附上自己写的列表排序函数
# -*- coding:utf-8 -*-
def list_sort(source_list,sorted_list):
i = 0
j = 0
while i < len(source_list):
item_init = source_list[i]
for item in source_list[i + 1:]:
if item_init < item:
item_init = item
#l_sort.append(item_init)
if source_list.count(item_init) >1:
while j <= source_list.count(item_init):
sorted_list.append(item_init)
index = source_list.index(item_init)
source_list.pop(index)
j += 1
else:
sorted_list.append(item_init)
source_list.pop(source_list.index(item_init))
j = 0
return sorted_list
if __name__ == '__main__':
source_list = [5, 9, 7, 6, 3, 1, 9, 40, 25, 15, 2, 10, 8, 4, 10, 8, 15, 20, 25, 40]
sorted_list = []
sorted_list = list_sort(source_list,sorted_list)
print(sorted_list)
列表的切片
1、正向切片,从0开始至len(list)
2、负向切片,从-1开始
# 使用负向切片,可以实现逆序排序,效果等同于reverse() 这个函数,而且不改变列表的原状态
In [133]: l1
Out[133]: [1, 2, 3, 5, 9, 10, 21, 31]
In [132]: l1[::-1]
Out[132]: [31, 21, 10, 9, 5, 3, 2, 1]
# 左闭右开
In [137]: l1[2:5]
Out[137]: [10, 9, 5]
# 步长为2
In [139]: l1[1:8:2]
Out[139]: [21, 9, 3, 1]
In [140]: l1
Out[140]: [31, 21, 10, 9, 5, 3, 2, 1]
In [145]: l1[-1]
Out[145]: 1
In [146]: l1
Out[146]: [31, 21, 10, 9, 5, 3, 2, 1]
# 反向索引,从右开始。左闭右开
In [147]: l1[-6:-1]
Out[147]: [10, 9, 5, 3, 2]
In [148]: l1
Out[148]: [31, 21, 10, 9, 5, 3, 2, 1]
In [149]: l1[-6:]
Out[149]: [10, 9, 5, 3, 2, 1]