在python中索引所有*除了*一项
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19286657/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Index all *except* one item in python
提问by choldgraf
Is there a simple way to index all elements of a list (or array, or whatever) exceptfor a particular index? E.g.,
除了特定索引之外,是否有一种简单的方法可以索引列表(或数组或其他)的所有元素?例如,
mylist[3]
will return the item in position 3milist[~3]
will return the whole list except for 3
mylist[3]
将返回位置 3 的项目milist[~3]
将返回整个列表,除了 3
采纳答案by tom10
For a list, you could use a list comp. For example, to make b
a copy of a
without the 3rd element:
对于list,您可以使用 list comp。例如,要制作没有第三个元素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]
This is very general, and can be used with all iterables, including numpy arrays. If you replace []
with ()
, b
will be an iterator instead of a list.
这是非常通用的,可以用于所有可迭代对象,包括 numpy 数组。如果替换[]
为()
,b
将是一个迭代器而不是一个列表。
Or you could do this in-place with pop
:
或者您可以使用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]
In numpyyou could do this with a boolean indexing:
在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])
which will, in general, be much faster than the list comprehension listed above.
通常,这比上面列出的列表理解要快得多。
回答by Andreas Jung
>>> 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]
>>>
See also
也可以看看
回答by Abhijit
If you are using numpy, the closest, I can think of is using a mask
如果您使用的是 numpy,我能想到的最接近的是使用掩码
>>> 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])
Something similar can be achieved using itertools
without numpy
使用itertools
without可以实现类似的东西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]
回答by Andre Soares
The simplest way I found was:
我发现的最简单的方法是:
mylist[:x]+mylist[x+1:]
that will produce your mylist
without the element at index x
.
这将产生mylist
没有 index 元素的你x
。
回答by aberger
If you don't know the index beforehand here is a function that will work
如果您事先不知道索引,这里有一个可以工作的函数
def reverse_index(l, index):
try:
l.pop(index)
return l
except IndexError:
return False
回答by brnl
Use np.delete
! It does not actually delete anything inplace
使用np.delete
!它实际上并没有就地删除任何内容
Example:
例子:
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]])
回答by Vlad Bezden
I'm going to provide a functional (immutable) way of doing it.
我将提供一种功能性(不可变)的方法。
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.compressfunction 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.filterfalsefunction 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]
标准且简单的方法是使用切片:
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}")
输出:
data: [0, 1, 2, 3, 4], new_data: [0, 1, 2, 4]
使用列表理解:
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}")
输出:
data: [0, 1, 2, 3, 4], new_data: [0, 1, 2, 4]
使用过滤功能:
index_to_remove = 3 data = [*range(5)] new_data = [*filter(lambda i: i != index_to_remove, data)]
输出:
data: [0, 1, 2, 3, 4], new_data: [0, 1, 2, 4]
使用掩码。屏蔽由标准库中的itertools.compress函数提供:
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}")
输出:
data: [0, 1, 2, 3, 4], mask: [1, 1, 1, 0, 1], new_data: [0, 1, 2, 4]
使用Python 标准库中的itertools.filterfalse函数
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}")
输出:
data: [0, 1, 2, 3, 4], new_data: [0, 1, 2, 4]
回答by shahar_m
Note that if variable is list of lists, some approaches would fail. For example:
请注意,如果变量是列表列表,则某些方法会失败。例如:
v1 = [[range(3)] for x in range(4)]
v2 = v1[:3]+v1[4:] # this fails
v2
For the general case, use
对于一般情况,使用
removed_index = 1
v1 = [[range(3)] for x in range(4)]
v2 = [x for i,x in enumerate(v1) if x!=removed_index]
v2