列表
lb = [1, 2, 3, 4, 5, 'dddddd']
lb[0]
正索引 |
0 |
1 |
2 |
3 |
4 |
4 |
复索引 |
-6 |
-5 |
-4 |
-3 |
-2 |
-1 |
值 |
1 |
2 |
3 |
4 |
5 |
‘dddddd’ |
len1 = len(lb)
lb[len1 - 1]
'dddddd'
lb[-1]
'dddddd'
lb[0:3]
[1, 2, 3]
lb[3:6]
[4, 5, 'dddddd']
lb[:2]
[1, 2]
lb[2:]
[3, 4, 5, 'dddddd']
lb[::2]
[1, 3, 5]
lb[::-1]
['dddddd', 5, 4, 3, 2, 1]
a = [1, 2]
a.append(3)
[1, 2, 3]
a.extend([4, 5, 6])
[1, 2, 3, 4, 5, 6]
a.insert(2, 100)
[1, 2, 100, 3, 4, 5, 6]
a.remove(100)
[1, 2, 3, 4, 5, 6]
a.pop(2)
3
[1, 2, 4, 5, 6]
a.clear()
[]
b = [0, 1, 2, 3, 4]
b[4] = 100
[0, 1, 2, 3, 100]
b[4:] = [5, 6]
[0, 1, 2, 3, 5, 6]
c = [4, 5, 9, 1, 0, 4]
c.sort()
[0, 1, 4, 4, 5, 9]
c.reverse()
[9, 5, 4, 4, 1, 0]
c = [4, 5, 9, 1, 0, 4]
c.sort(reverse = True)
[9, 5, 4, 4, 1, 0]
c.count(4)
2
c.index(4)
2
p = [2, 4]
q = [5, 6]
p + q
[2, 4, 5, 6]
p * 3
[2, 4, 2, 4, 2, 4]
matrix = [[1, 2, 3], [4, 5, 6]]
A = [0] * 3
for i in range(3):
A[i] = [0] * 3
[[0, 0, 0], [0, 0, 0], [0, 0, 0]]
- is


A[0] is A[1]
False
B = [[0] * 3] * 3
B[0] is B[1]
True
x = "FishC"
y = "FishC"
x is y
True

- 浅拷贝 和 深拷贝

x = [1, 2, 3]
xx = x.copy()
xxx = x[:]
xx, xxx
([1, 100, 3], [1, 100, 3])

import copy
x = [[1, 2], [3, 4]]
y = copy.copy(x)
x[1] is y[1]
True
x = [[1, 2], [3, 4]]
y = copy.deepcopy(x)
x[1] is y[1]
False
oho = [1, 2, 3, 4, 5]
oho = [i * 2 for i in oho]
[2, 4, 6, 8, 10]
even = [i for i in range(10) if i % 2 == 0]
[0, 2, 4, 6, 8]
matrix = [[1, 2, 3], [4, 5, 6]]
f = [j for i in matrix for j in i]
[1, 2, 3, 4, 5, 6]