iron2man
iron2man

Reputation: 1827

Producing array of which its elements are indices

I'm trying to remove values from a large data set that are inconsistent in my analysis.

Here is the current method i'm started with.

For example, lets say I have an array a that consist of an number of elements.

a = [30, 40, 200, 324, 8, 67, 789, 9, 567, 2143, 13]
idx = [(i,value) for i,value in enumerate(a) if value<=10]

print idx
>>> [(4, 8), (7, 9)]

How do i go about to where I can only just create an array that consist of only its indices

print idx
>>> [4, 8]

Upvotes: 0

Views: 68

Answers (2)

Mike M&#252;ller
Mike M&#252;ller

Reputation: 85462

You are almost there, just only use the index:

>>> a = [30, 40, 200, 324, 8, 67, 789, 9, 567, 2143, 13]
>>> idx = [i for i, value in enumerate(a) if value<=10]
>>> idx
[4, 7]

Upvotes: 0

Moses Koledoye
Moses Koledoye

Reputation: 78556

Don't include the value in the comprehension result:

idx = [i for i, v in enumerate(a) if v <= 10]

Upvotes: 2

Related Questions