clean way prepend an element to a numpy array

B

bdb112

If I want to add an element at the beginning of an array, it seems
like I must make a list, insert in place, then make an array again.

Of course, lists can be efficient too, and the insert() funtion works
nicely in that case, but sometimes arrays are the best choice

e.g.
x=array([1,2,3])
# to put the element 0 at the head of the array
listx=list(x)
listx.insert(0,0)
x=array(listx)
# this extra effort distracts from the clarity of the code, and must
slow things a little.

# While I appreciate that array operations that cause memory re-
allocation and copying are to be
# avoided if at all possible, sometimes this is the best compromise
between clarity and efficiency

# A nicer piece of code would be

x.insert(0,0)
#or
x.prepend(0)

# It is a little easier to grow an array at the end (if it is not
referenced)
x.resize(4)

I saw this interest syntax on this site
x[:0]=0

I guess that is cute, but could be confusing....(and doesn't work)
 
B

bdb112

The NumPy ‘ndarray’ type (which is what you get by default from the
‘array’ factory function) is a far more complex type than (and is not
derived from) the Python list.

For manipulating them, you'll want to study the NumPy documentation
<URL:http://docs.scipy.org/doc/numpy/reference/arrays.ndarray.html>.

Yes, I had had a look through that - nothing there that allows writing
a succint
clear statement (except for my .resize example in the original post),
although if efficiency was
a concern, then maybe resize and shift right, but that would really
obscure the code.
 
R

Robert Kern

If I want to add an element at the beginning of an array, it seems
like I must make a list, insert in place, then make an array again.

the_array = numpy.concatenate([new_value, the_array])

You will want to ask numpy questions on the numpy mailing list.

http://www.scipy.org/Mailing_Lists

--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
that is made terrible by our own mad attempt to interpret it as though it had
an underlying truth."
-- Umberto Eco
 
G

greg

bdb112 said:
I saw this interest syntax on this site
x[:0]=0

I guess that is cute, but could be confusing....(and doesn't work)

It does if you use an array of the appropriate
type on the right hand side:

a[:0] = array('i', [0])
 
R

Robert Kern

bdb112 said:
I saw this interest syntax on this site
x[:0]=0

I guess that is cute, but could be confusing....(and doesn't work)

It does if you use an array of the appropriate
type on the right hand side:

a[:0] = array('i', [0])

Not when 'a' is a numpy array rather than an array.array.

--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
that is made terrible by our own mad attempt to interpret it as though it had
an underlying truth."
-- Umberto Eco
 
G

greg

Robert said:
a[:0] = array('i', [0])

Not when 'a' is a numpy array rather than an array.array.

That's true, but I got the impression that the OP was
talking about array.array, not numpy.array.

It's very confusing having two widely-used types
both called 'array'. :-(
 
R

Robert Kern

Robert said:
a[:0] = array('i', [0])

Not when 'a' is a numpy array rather than an array.array.

That's true, but I got the impression that the OP was
talking about array.array, not numpy.array.

Did you read the Subject: line? :)

--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
that is made terrible by our own mad attempt to interpret it as though it had
an underlying truth."
-- Umberto Eco
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,143
Latest member
SterlingLa
Top