使用有条件列表理解获取索引

发布于 2025-01-28 19:24:26 字数 346 浏览 0 评论 0原文

我有以下NP.array:

my_array=np.array([False, False, False, True, True, True, False, True, False, False, False, False, True])

如何使用与真实元素相对应的索引的列表理解列表。 的输出将是[3,4,5,7,12]

我要寻找

cols = [index if feature_condition==True for index, feature_condition in enumerate(my_array)]

在这种情况下,

I have the following np.array:

my_array=np.array([False, False, False, True, True, True, False, True, False, False, False, False, True])

How can I make a list using list comprehension of the indices corresponding to the True elements. In this case the output I'm looking for would be [3,4,5,7,12]

I've tried the following:

cols = [index if feature_condition==True for index, feature_condition in enumerate(my_array)]

But is not working

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

为什么要特别是列表理解?

>>> np.where(my_array==True)
(array([ 3,  4,  5,  7, 12]),)

这可以完成工作,并且更快。列表解决方案将是:

>>> [index for index, feature_condition in enumerate(my_array) if feature_condition == True]
[3, 4, 5, 7, 12]

可接受的答案解释了订购的混乱:在列表中理解

我对差异感到好奇:

def np_time(array):
     np.where(my_array==True)
def list_time(array):
    [index for index, feature_condition in enumerate(my_array) if feature_condition == True]

timeit.timeit(lambda: list_time(my_array),number = 1000)
0.007574789000500459
timeit.timeit(lambda: np_time(my_array),number = 1000)
0.0010812399996211752

why specifically a list comprehension?

>>> np.where(my_array==True)
(array([ 3,  4,  5,  7, 12]),)

this does the job and is quicker. The list solution would be:

>>> [index for index, feature_condition in enumerate(my_array) if feature_condition == True]
[3, 4, 5, 7, 12]

Accepted answer of this explains the confusion of the ordering: if/else in a list comprehension

I was curious of the differences:

def np_time(array):
     np.where(my_array==True)
def list_time(array):
    [index for index, feature_condition in enumerate(my_array) if feature_condition == True]

timeit.timeit(lambda: list_time(my_array),number = 1000)
0.007574789000500459
timeit.timeit(lambda: np_time(my_array),number = 1000)
0.0010812399996211752

献世佛 2025-02-04 19:24:26

如果不正确的顺序,则应在最后 -

$more numpy_1.py
import numpy as np
my_array=np.array([False, False, False, True, True, True, False, True, False, False, False, False, True])
print (my_array)

cols = [index for index, feature_condition in enumerate(my_array) if feature_condition]

print (cols)

$python numpy_1.py
[False False False  True  True  True False  True False False False False
  True]
[3, 4, 5, 7, 12]

The order of if is not correct, should be in last -

$more numpy_1.py
import numpy as np
my_array=np.array([False, False, False, True, True, True, False, True, False, False, False, False, True])
print (my_array)

cols = [index for index, feature_condition in enumerate(my_array) if feature_condition]

print (cols)

$python numpy_1.py
[False False False  True  True  True False  True False False False False
  True]
[3, 4, 5, 7, 12]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文