32

Given the following vector,

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

I need to identify the indices of "a" whose elements are >= than 4, like this:

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

The info in "idx" will be used to delete the elements from another list X (X has the same number of elements that "a"):

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

I heard that numpy might help. Any ideas? Thanks!

4

6 回答 6

39
>>> [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]
于 2012-12-05T06:23:37.610 回答
19

好的,我明白你的意思,一行 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]
于 2012-12-05T15:57:28.303 回答
8
>>> 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])
于 2012-12-05T06:24:53.213 回答
8

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

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

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

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

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

于 2017-03-14T09:11:12.637 回答
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]
于 2019-09-23T11:47:15.617 回答
1

使用过滤器内置功能很好

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

解释

过滤器(有趣,可迭代)

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

λx:x > 4

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

您的解决方案

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

>>> a = [1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> filter(lambda x: x<4 ,a)
[1, 2, 3]
于 2012-12-05T06:25:00.917 回答