使用有条件列表理解获取索引
我有以下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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
为什么要特别是列表理解?
这可以完成工作,并且更快。列表解决方案将是:
可接受的答案解释了订购的混乱:在列表中理解
我对差异感到好奇:
why specifically a list comprehension?
this does the job and is quicker. The list solution would be:
Accepted answer of this explains the confusion of the ordering: if/else in a list comprehension
I was curious of the differences:
如果不正确的顺序,则应在最后 -
The order of if is not correct, should be in last -