找到大于x的元素的索引

Oli*_*sen 20 python indexing logical-operators indices

鉴于以下向量,

a = [1, 2, 3, 4, 5, 6, 7, 8, 9]
Run Code Online (Sandbox Code Playgroud)

我需要识别其元素> = 4的"a"的索引,如下所示:

idx = [3, 4, 5, 6, 7, 8] 
Run Code Online (Sandbox Code Playgroud)

"idx"中的信息将用于删除另一个列表X中的元素(X具有与"a"相同数量的元素):

del X[idx] #idx is used to delete these elements in X. But so far isn't working.
Run Code Online (Sandbox Code Playgroud)

我听说numpy可能会有所帮助.有任何想法吗?谢谢!

Aes*_*ete 26

>>> [i for i,v in enumerate(a) if v > 4]
[4, 5, 6, 7, 8]
Run Code Online (Sandbox Code Playgroud)

enumerate返回数组中每个项的索引和值.因此,如果值v大于4,则i在新数组中包含索引.

或者您可以只修改列表并排除上面的所有值4.

>>> a[:] = [x for x in a if x<=4]
>>> a 
[1, 2, 3, 4]
Run Code Online (Sandbox Code Playgroud)


Sha*_*ang 15

好的,我理解你的意思,单行Python就足够了:

使用列表理解

[ j for (i,j) in zip(a,x) if i >= 4 ]
# a will be the list compare to 4
# x another list with same length

Explanation:
>>> a
[1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> x
['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'j']
Run Code Online (Sandbox Code Playgroud)

Zip函数将返回元组列表

>>> zip(a,x)
[(1, 'a'), (2, 'b'), (3, 'c'), (4, 'd'), (5, 'e'), (6, 'f'), (7, 'g'), (8, 'h'), (9, 'j')]
Run Code Online (Sandbox Code Playgroud)

列表理解是一个快捷方式遍历列表中的元素,其"中"后,并评估与表达的元素,然后将结果返回到列表中,你也可以添加从而导致要返回条件

>>> [expression(element) for **element** in **list** if condition ]
Run Code Online (Sandbox Code Playgroud)

此代码除了返回拉链的所有对之外什么都不做.

>>> [(i,j) for (i,j) in zip(a,x)]
[(1, 'a'), (2, 'b'), (3, 'c'), (4, 'd'), (5, 'e'), (6, 'f'), (7, 'g'), (8, 'h'), (9, 'j')]
Run Code Online (Sandbox Code Playgroud)

我们所做的是通过指定"if"后跟布尔表达式来添加条件

>>> [(i,j) for (i,j) in zip(a,x) if i >= 4]
[(4, 'd'), (5, 'e'), (6, 'f'), (7, 'g'), (8, 'h'), (9, 'j')]
Run Code Online (Sandbox Code Playgroud)

使用Itertools

>>> [ _ for _ in itertools.compress(d, map(lambda x: x>=4,a)) ]
# a will be the list compare to 4
# d another list with same length
Run Code Online (Sandbox Code Playgroud)

在Python中使用带有单行的 itertools.compress 来完成关闭此任务

>>> a = [1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> d = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'j'] # another list with same length
>>> map(lambda x: x>=4, a)  # this will return a boolean list 
[False, False, False, True, True, True, True, True, True]


>>> import itertools
>>> itertools.compress(d, map(lambda x: x>4, a)) # magic here !
<itertools.compress object at 0xa1a764c>     # compress will match pair from list a and the boolean list, if item in boolean list is true, then item in list a will be remain ,else will be dropped
#below single line is enough to solve your problem
>>> [ _ for _ in itertools.compress(d, map(lambda x: x>=4,a)) ] # iterate the result.
['d', 'e', 'f', 'g', 'h', 'j']
Run Code Online (Sandbox Code Playgroud)

itertools.compress的解释,我想这对你的理解很清楚:

>>> [ _ for _ in itertools.compress([1,2,3,4,5],[False,True,True,False,True]) ]
[2, 3, 5]
Run Code Online (Sandbox Code Playgroud)


Oka*_*575 8

在我看来最简单的就是使用 numpy

X[np.array(a)>4]#X needs to be np.array as well
Run Code Online (Sandbox Code Playgroud)

说明: np.array 将 a 转换为数组。

np.array(a)>4 给出了一个包含所有应该保留的元素的 bool 数组

并且 X 被 bool 数组过滤,因此只选择 a 大于 4 的元素(其余的被丢弃)


Jor*_*ley 6

>>> import numpy as np
>>> a = np.array(range(1,10))
>>> indices = [i for i,v in enumerate(a >= 4) if v]
>>> indices
[3, 4, 5, 6, 7, 8]

>>> mask = a >= 4
>>> mask
array([False, False, False,  True,  True,  True,  True,  True,  True], dtype=boo
l)
>>> a[mask]
array([4, 5, 6, 7, 8, 9])
>>> np.setdiff1d(a,a[mask])
array([1, 2, 3])
Run Code Online (Sandbox Code Playgroud)


sin*_*ium 6

我想我来得有点晚了(虽然使用 Numpy 事情变得更容易了)..

import numpy as np

# Create your array
a = np.arange(1, 10)
# a = array([1, 2, 3, 4, 5, 6, 7, 8, 9])

# Get the indexes/indices of elements greater than 4 
idx = np.where(a > 4)[0]
# idx = array([4, 5, 6, 7, 8])

# Get the elements of the array that are greater than 4
elts = a[a > 4]
# elts = array([5, 6, 7, 8, 9])

# Convert idx(or elts) to a list
idx = list(idx)
#idx = [4, 5, 6, 7, 8]
Run Code Online (Sandbox Code Playgroud)