0

This shouldn't be that hard. Assume I have a 2D array:

a = [['1' 'George']
     ['5' ' ']
     ['7' 'Jose']
     ['5' ' ']
     ['7','Fred']]

I wish to find all the indexed values where a[:,1] == ' '

My best guess is:

missing_vals = a[a[:,' ']==' '
a[missing_vals] 

I don't want the answer:

   ['5','5'] 

but the answer:

[1,4]

Meaning the 2nd and 5th elements of the array.

Thanks.

1 Answer 1

1

This is what you are looking for:

>>> a = [['1', 'George'],
...       ['5', ' '],
...       ['7', 'Jose'],
...       ['5', ' '],
...       ['7','Fred']]
>>> [i for i, [k,v] in enumerate(a) if v == ' ']
[1, 3]

Explanation:

We are asking for all indexes i in list a at which the element [k,v] in a has the element v equal to a space.

Sign up to request clarification or add additional context in comments.

2 Comments

I am sorry. I did not specify. What I seek is the position of these in the array? I am not looking for the answer ['5','5'] but this answer: [1,4] . Meaning that if the array starts at 0, the positions for which the second column a == ' '.
No worries! I rewrote the answer for you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.