找到大于x的元素的索引

时间:2012-12-05 06:21:23

标签: python indexing logical-operators indices

给出以下向量,

a = [1, 2, 3, 4, 5, 6, 7, 8, 9]

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

idx = [3, 4, 5, 6, 7, 8] 

“idx”中的信息将用于删除其他列表中的元素X(X具有与“a”相同数量的元素):

del X[idx] #idx is used to delete these elements in X. But so far isn't working.

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

6 个答案:

答案 0 :(得分:25)

>>> [i for i,v in enumerate(a) if v > 4]
[4, 5, 6, 7, 8]

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

或者,您只需修改列表并排除4以上的所有值。

>>> a[:] = [x for x in a if x<=4]
>>> a 
[1, 2, 3, 4]

答案 1 :(得分: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']

Zip函数将返回元组列表

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

列表理解是将元素循环到列表中的快捷方式,在“in”之后,并使用表达式计算元素,然后将结果返回到列表,还可以添加要返回的结果的条件

>>> [expression(element) for **element** in **list** if condition ]

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

>>> [(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')]

我们所做的是通过指定“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')]

使用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

在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']

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

>>> [ _ for _ in itertools.compress([1,2,3,4,5],[False,True,True,False,True]) ]
[2, 3, 5]

答案 2 :(得分: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])

答案 3 :(得分:4)

我眼中最简单的就是使用numpy

X[np.array(a)>4]#X needs to be np.array as well

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

np.array(a)&gt; 4给出了一个bool数组,其中包含了应该保留的所有元素

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

答案 4 :(得分:1)

使用过滤器内置功能很好

>>>a = [1, 2, 3, 4, 5, 6, 7, 8, 9]
>>>filter(lambda x : x < 4, a)
[1, 2, 3]

<强>解释

过滤(FUN,Iterable)

此表达式将迭代Iterable中的所有元素并将其作为参数提供给FUN函数,如果return为True,则该arugment将附加到内部列表

lambda x:x&gt; 4

这意味着一个匿名函数将接受一个参数并测试它大于4,并返回True的假值

您的解决方案

如果您尝试删除大于4的所有元素,请尝试打击

>>> a = [1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> filter(lambda x: x<4 ,a)
[1, 2, 3]

答案 5 :(得分:1)

我想我来这里有点晚了(虽然使用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]