numpy:指定多维数组的轴
你对numpy做的很接近,但你用错了轴。如果指定axis=0,则会得到所需的结果。>>> import numpy as np
>>> l = [(1, 2), (3, 4), (5, 6), (7, 8), (9, 10)]
>>> l = np.array(l)
>>> l
array([[ 1, 2],
[ 3, 4],
[ 5, 6],
[ 7, 8],
[ 9, 10]])
>>> np.delete(l, [2,3], axis=0)
array([[ 1, 2],
[ 3, 4],
[ 9, 10]])
香草Python
对于逐步改变列表的操作,重要的是按索引的降序进行,例如[3, 2],而不是[2, 3]:
使用pop()>>> l = [(1, 2), (3, 4), (5, 6), (7, 8), (9, 10)]
>>> l.pop(3)
(7, 8)
>>> l.pop(2)
(5, 6)
>>> l
[(1, 2), (3, 4), (9, 10)]
使用del>>> l = [(1, 2), (3, 4), (5, 6), (7, 8), (9, 10)]
>>> for i in [3, 2]:
... del l[i]
...
>>> l
[(1, 2), (3, 4), (9, 10)]
使用remove()>>> l = [(1, 2), (3, 4), (5, 6), (7, 8), (9, 10)]
>>> remove_list = [l[2], l[3]]
>>> remove_list
[(5, 6), (7, 8)]
>>> for r in remove_list:
... l.remove(r)
...
>>> l
[(1, 2), (3, 4), (9, 10)]