- 列表特性
list.append(x)
>>> fruits = ['orange', 'apple', 'pear', 'banana', 'kiwi', 'apple', 'banana'] >>> fruits.append('xigua') >>> fruits ['orange', 'apple', 'pear', 'banana', 'kiwi', 'apple', 'banana', 'xigua'] |
list.extend(iterable)
>>> fruits.extend(['nangua','donggua']) >>> fruits ['orange', 'apple', 'pear', 'banana', 'kiwi', 'apple', 'banana', 'xigua', 'nangua', 'donggua'] |
list.insert(index,x) 黄色高亮方法返回值为None
>>> fruits.insert(0,'fruits') >>> fruits ['fruits', 'orange', 'apple', 'pear', 'banana', 'kiwi', 'apple', 'banana', 'xigua', 'nangua', 'donggua'] |
list.remove(x) 没有匹配项则报错
>>> fruits.remove('xigua') >>> fruits ['fruits', 'orange', 'apple', 'pear', 'banana', 'kiwi', 'apple', 'banana', 'nangua', 'donggua'] >>> fruits.remove('wolf') Traceback (most recent call last): File "<pyshell#126>", line 1, in <module> fruits.remove('wolf') ValueError: list.remove(x): x not in list |
list.pop([i]) 指定索引越界则报错,默认删除最后一个
>>> fruits.pop() 'donggua' >>> fruits ['fruits', 'orange', 'apple', 'pear', 'banana', 'kiwi', 'apple', 'banana', 'nangua'] >>> fruits.pop(8) 'nangua' >>> fruits.pop(10) Traceback (most recent call last): File "<pyshell#130>", line 1, in <module> fruits.pop(10) IndexError: pop index out of range |
list.clear() <==>del a[:]
>>> fruits ['apple', 'apple', 'apple', 'banana', 'banana', 'fruits', 'kiwi', 'orange', 'pear'] >>> fruits.clear() >>> fruits [] >>> fruits = ['orange', 'apple', 'pear', 'banana', 'kiwi', 'apple', 'banana'] >>> del fruits[:] >>> fruits [] |
list.index(x[,start[,end]]) 返回第一个匹配值,或没有匹配值报错
>>> fruits.index('nangua') Traceback (most recent call last): File "<pyshell#131>", line 1, in <module> fruits.index('nangua') ValueError: 'nangua' is not in list >>> fruits.index('apple') 2 >>> fruits.index('apple',0,2) Traceback (most recent call last): File "<pyshell#133>", line 1, in <module> fruits.index('apple',0,2) ValueError: 'apple' is not in list |
list.count(x) 不存在的值报错
>>> fruits.count('apple') 3 >>> frutis.count('nangua') Traceback (most recent call last): File "<pyshell#136>", line 1, in <module> frutis.count('nangua') NameError: name 'frutis' is not defined |
list.sort(key=None,reverse=False)
>>> fruits.sort() >>> fruits ['apple', 'apple', 'apple', 'banana', 'banana', 'fruits', 'kiwi', 'orange', 'pear'] >>> fruits.sort(key=lambda fruit:fruit[1]) >>> fruits ['banana', 'banana', 'pear', 'kiwi', 'apple', 'apple', 'apple', 'fruits', 'orange'] >>> fruits.sort(reverse=True) >>> fruits ['pear', 'orange', 'kiwi', 'fruits', 'banana', 'banana', 'apple', 'apple', 'apple'] |
list.reverse()
>>> fruits ['pear', 'orange', 'kiwi', 'fruits', 'banana', 'banana', 'apple', 'apple', 'apple'] >>> fruits.reverse() >>> fruits ['apple', 'apple', 'apple', 'banana', 'banana', 'fruits', 'kiwi', 'orange', 'pear'] |
list.copy()
>>> a=[1,2,3] >>> b=a >>> id(a) 2711896985480 >>> id(b) 2711896985480 >>> c=a.copy() >>> id(c) 2711897298312 |
- append() and pop() 将列表但做栈使用
- 列表作为队列使用
在列表的末尾添加和弹出元素非常快,但是在列表的开头插入或弹出元素却很慢 (因为所有的其他元素都必须移动一位)。
若要实现一个队列, collections.deque 被设计用于快速地从两端操作。
deque对象
class collections.deque([iterable[, maxlen]])
Deque 支持线程安全,内存高效添加(append)和弹出(pop),从两端都可以,两个方向的大概开销都是 O(1) 复杂度。
如果 maxlen 没有指定或者是 None ,deques 可以增长到任意长度。否则,deque就限定到指定最大长度。一旦限定长度的deque满了,当新项加入时,同样数量的项就从另一端弹出。
方法:
deque.apeed(x)
deque.apeedleft(x)
deque.extend(iterable)
deque.extendleft(iterable)
deque.pop()
deque.popleft()
deque.clear()
deque.copy()
deque.count(x)
deque.index(x)
deque.remove(x)
deque.reverse()
deque.rotate(n=1) 向右循环移动n步,如果n是负数,则向左循环
reversed(d)
copy.copy(d)
copy.deepcopy(d)
示例:
>>> from collections import deque >>> d=deque('abc') >>> d deque(['a', 'b', 'c']) >>> d.append('d') >>> d.appendleft('z') >>> d deque(['z', 'a', 'b', 'c', 'd']) >>> d.extend(['e','f']) >>> d deque(['z', 'a', 'b', 'c', 'd', 'e', 'f']) >>> d.extendleft(['w','y']) >>> d deque(['y', 'w', 'z', 'a', 'b', 'c', 'd', 'e', 'f']) >
|