有没有一种简单的方法来索引列表(或数组,或其他任何东西)中除特定索引外的所有元素?例如,
mylist[3]
将返回在第3个位置的项目milist[~3]
将返回除了3以外的整个列表
对于一个列表,你可以使用列表推导式。例如,要使b
成为a
的副本但不包括第三个元素:
a = range(10)[::-1] # [9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
b = [x for i,x in enumerate(a) if i!=3] # [9, 8, 7, 5, 4, 3, 2, 1, 0]
这是非常通用的,可以与所有可迭代对象一起使用,包括numpy数组。如果将[]
替换为()
,则b
将成为迭代器而不是列表。
或者您可以使用pop
就地完成此操作:
a = range(10)[::-1] # a = [9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
a.pop(3) # a = [9, 8, 7, 5, 4, 3, 2, 1, 0]
在 numpy 中,您可以使用布尔索引来执行此操作:
a = np.arange(9, -1, -1) # a = array([9, 8, 7, 6, 5, 4, 3, 2, 1, 0])
b = a[np.arange(len(a))!=3] # b = array([9, 8, 7, 5, 4, 3, 2, 1, 0])
一般情况下,这将比上面列出的列表推导式快得多。
我找到的最简单的方法是:
mylist[:x] + mylist[x+1:]
这将生成您的mylist
,但不包括索引为x
的元素。
mylist = [0, 1, 2, 3, 4, 5]
x = 3
mylist[:x] + mylist[x+1:]
生成的结果
mylist = [0, 1, 2, 4, 5]
exc = lambda s, i: s[:i] + s[i+1:]
exc(mylist, 3)
x==5
,它不会引发IndexError。在Python中,列表切片越界不会导致错误,因为当切片的起始/结束索引大于列表的长度时,它们会被简单地减少到列表的长度。来源:Python文档注4 - Gustin>>> l = range(1,10)
>>> l
[1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> l[:2]
[1, 2]
>>> l[3:]
[4, 5, 6, 7, 8, 9]
>>> l[:2] + l[3:]
[1, 2, 4, 5, 6, 7, 8, 9]
>>>
另请参阅
numpy.concatenate
。 - Bi Rico如果您正在使用numpy,我能想到的最接近的方法是使用掩码(mask)
>>> import numpy as np
>>> arr = np.arange(1,10)
>>> mask = np.ones(arr.shape,dtype=bool)
>>> mask[5]=0
>>> arr[mask]
array([1, 2, 3, 4, 5, 7, 8, 9])
使用 itertools
也可以实现类似的功能,而无需使用 numpy
>>> from itertools import compress
>>> arr = range(1,10)
>>> mask = [1]*len(arr)
>>> mask[5]=0
>>> list(compress(arr,mask))
[1, 2, 3, 4, 5, 7, 8, 9]
np.arange(len(arr)) != 3
的掩码,因为这样可以内联处理,例如 arr[~(np.arange(len(arr)) == 3)]
或其他方式。 - DSM使用np.delete
!它实际上并没有原地删除任何东西。
在你的例子中,“mylist [〜3]”将会被写成这样:mylist.delete(3)
一个更复杂的例子:
import numpy as np
a = np.array([[1,4],[5,7],[3,1]])
# a: array([[1, 4],
# [5, 7],
# [3, 1]])
ind = np.array([0,1])
# ind: array([0, 1])
# a[ind]: array([[1, 4],
# [5, 7]])
all_except_index = np.delete(a, ind, axis=0)
# all_except_index: array([[3, 1]])
# a: (still the same): array([[1, 4],
# [5, 7],
# [3, 1]])
The standard and easy way of doing it is to use slicing:
index_to_remove = 3
data = [*range(5)]
new_data = data[:index_to_remove] + data[index_to_remove + 1:]
print(f"data: {data}, new_data: {new_data}")
Output:
data: [0, 1, 2, 3, 4], new_data: [0, 1, 2, 4]
Use list comprehension:
data = [*range(5)]
new_data = [v for i, v in enumerate(data) if i != index_to_remove]
print(f"data: {data}, new_data: {new_data}")
Output:
data: [0, 1, 2, 3, 4], new_data: [0, 1, 2, 4]
Use filter function:
index_to_remove = 3
data = [*range(5)]
new_data = [*filter(lambda i: i != index_to_remove, data)]
Output:
data: [0, 1, 2, 3, 4], new_data: [0, 1, 2, 4]
Using masking. Masking is provided by itertools.compress function in the standard library:
from itertools import compress
index_to_remove = 3
data = [*range(5)]
mask = [1] * len(data)
mask[index_to_remove] = 0
new_data = [*compress(data, mask)]
print(f"data: {data}, mask: {mask}, new_data: {new_data}")
Output:
data: [0, 1, 2, 3, 4], mask: [1, 1, 1, 0, 1], new_data: [0, 1, 2, 4]
Use itertools.filterfalse function from Python standard library
from itertools import filterfalse
index_to_remove = 3
data = [*range(5)]
new_data = [*filterfalse(lambda i: i == index_to_remove, data)]
print(f"data: {data}, new_data: {new_data}")
Output:
data: [0, 1, 2, 3, 4], new_data: [0, 1, 2, 4]
对于一维的numpy
数组,使用np.concatenate
方法比使用np.arange
方法更快。
基准测试:
x = np.arange(1000) * 2
i = 3
%timeit np.concatenate((x[:i],x[i+1:]))
%timeit x[np.arange(len(x))!=i]
# 9.21 µs ± 467 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)
# 32.8 µs ± 7.46 µs per loop (mean ± std. dev. of 7 runs, 10000 loops each)
idxs = list(range(len(array))).remove(idx)
array[idxs]
这应该可以工作,它会移除不必要的索引,然后切割数组
def reverse_index(l, index):
try:
l.pop(index)
return l
except IndexError:
return False
list = ["This", "is", "a", "list"]
listnolast = list[:-1]
listnofirst = list[1:]