1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
|
### 基本操作
```python >>> l = [1, 3, 3] >>> l[0] 1 >>> l.append(1) >>> l [1, 3, 3, 1] >>> l.append([1, 2, 3]) >>> l [1, 3, 3, 1, [1, 2, 3]] >>> l.append(1, 2, 3) Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: append() takes exactly one argument (3 given) >>> l.extend([1, 2, 3]) >>> l [1, 3, 3, 1, [1, 2, 3], 1, 2, 3] >>> l.index(1) 0 >>> l.index(1,2) 3 >>> l.insert(2,'c') >>> l [1, 3, 'c', 3, 1, [1, 2, 3], 1, 2, 3] >>> l.remove([1, 2, 3]) >>> l [1, 3, 'c', 3, 1, 1, 2, 3] >>> l.pop() 3 >>> l [1, 3, 'c', 3, 1, 1, 2] >>> l.count(1) 3 >>> l.sort() Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: '<' not supported between instances of 'str' and 'int' >>> l.remove('c') >>> l.sort() >>> l [1, 1, 1, 2, 3, 3] >>> l.sort(reverse=True) >>> l [3, 3, 2, 1, 1, 1] >>> l.reverse() >>> l [1, 1, 1, 2, 3, 3] >>> l += [4, 5, 6] >>> l [1, 1, 1, 2, 3, 3, 4, 5, 6] >>> l *= 2 >>> l [1, 1, 1, 2, 3, 3, 4, 5, 6, 1, 1, 1, 2, 3, 3, 4, 5, 6] >>> l[10:] [1, 1, 2, 3, 3, 4, 5, 6] >>> l[:10] [1, 1, 1, 2, 3, 3, 4, 5, 6, 1] >>> l[11:-4] [1, 2, 3] >>> l[::5] [1, 3, 1, 4] >>> [i for i in range(10) if i % 2 == 0] [0, 2, 4, 6, 8] >>> li = [1, 2] >>> [elem*2 for elem in li if elem>1] [4] >>> l.pop(0) 1 >>>
|