列表的方法
list.append(x)
a = [1, 2, 3, 4, 5]
a.append('a')
a.append(3)
a.append([1, 2, 3])
a.append({1, 2, 3})
a.append((1, 2, 3))
a.append({'b': 1})
print(a)
[1, 2, 3, 4, 5, 'a', 3, [1, 2, 3], {1, 2, 3}, (1, 2, 3), {'b': 1}]
a[len(a):] = [10]
list.extend(iterable)
a = [1, 2, 3, 4, 5]
a.extend('a')
a.extend([1, 2, 3])
a.extend({1, 2, 3})
a.extend((1, 2, 3))
a.extend({'b': 1})
print(a)
[1, 2, 3, 4, 5, 'a', 1, 2, 3, 1, 2, 3, 1, 2, 3, 'b']
a[len(a):] = range(3)
list.insert(i, x)
a = [1, 2, 3, 4, 5]
a.insert(0, 'a')
a.insert(0, [1, 2, 3])
a.insert(0, {1, 2, 3})
a.insert(0, (1, 2, 3))
a.insert(0, {'b': 1})
a.insert(0, 1)
print(a)
[1, {'b': 1}, (1, 2, 3), {1, 2, 3}, [1, 2, 3], 'a', 1, 2, 3, 4, 5]
list.remove(x)
a = [1, 2, 3, 4, 5]
a.remove(1)
print(a)
[2, 3, 4, 5]
list.pop([i])
a = [1, 2, 3, 4, 5]
p = a.pop()
print(p)
print(a)
5
[1, 2, 3, 4]
p = a.pop(2)
print(p)
print(a)
3
[1, 2, 4, 5]
list.clear()
a = [1, 2, 3, 4, 5]
a.clear()
print(a)
[]
del a[:]
print(a)
[]
list.index(x[, start[, end]])
a = [1, 1, 1, 2, 3, 4, 1]
print(a.index(1))
print(a.index(1, 2, 10))
0
2
list.count(x)
a = [1, 1, 1, 2, 3, 4, 1]
print(a.count(1))
print(a.count(2))
4
1
list.sort(*, key=None, reverse=False)
a = [1, 1, 1, 2, 3, 4, 1]
a.sort()
print(a)
[1, 1, 1, 1, 2, 3, 4]
a.sort(reverse=True)
print(a)
[4, 3, 2, 1, 1, 1, 1]
list.reverse()
a = [1, 2, 3, 'a', 'b', 'c']
a.reverse()
print(a)
['c', 'b', 'a', 3, 2, 1]
list.copy()
a = [1, 2, 3, 'a', 'b', 'c']
print(id(a))
a1 = a.copy()
print(a1)
print(id(a1))
1576055219456
[1, 2, 3, 'a', 'b', 'c']
1576055172480