3

How can I select multiple elements from a python array? I know that this is possible in numpy array but I cannot use numpy array in this case. I want to select certain elements of an array similar to masked array usage but i get following error in python

 nonzero = numpyarray.nonzero()
 pythonarray[nonzero] = numpyarray[nonzero]
 *** TypeError: only integer arrays with one element can be converted to an index

numpyarray[nonzero] works fine but I cannot access pythonarray[nonzero]. Is there a way to do this in python array?

1 Answer 1

1

Just use a loop:

for idx in nonzero:
    pythonarray[idx] = numpyarray[idx]

Numpy arrays probably support n-ary indices to maximize performance; Python lists don't go that far with optimisations so you have to use the simpler approach.

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

11 Comments

Thanks. I guess this will slow down the performance a lot. I have to do this 1 million times. And oh btw, it should be idx in the assignment expression instead of nonzero
@Alok If you need to do something a million times quickly, Python in general might not be a good choice. (If you can't keep this in C code using numpy.)
@Alok: Are you sure that Python lists resize faster than numpy arrays? As far as I know Python lists are backed by C arrays, so they have to be reallocated and possibly copied as well. If you need to grow efficiently to a large size without a reasonable upper bound, I'd consider using some sort of C-based linked list implementation to get rid of all the reallocations.
@Alok Also, if every line has exactly 30 elements, what you could do is have a Python / linked list of 30-element numpy arrays, one per line. (The IO cost should dominate the overhead of a linked list.) Once you read the whole file, you know how many elements there are. You could then either create one huge numpy array for the result and copy stuff over once from the list, or just use the list-of-arrays directly. (Which one is better depends on your use case.)
@Alok: you could ask a separate question about how to read millions lines appending 30 elements afterwards efficiently (provide a small working example even if it is too slow).
|

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.