[Numpy-discussion] azip

Alan G Isaac alan.isaac at gmail.com
Thu Jul 18 12:57:19 EDT 2013


I'm floating this thought even though it is not fleshed out.

On occasion, I run into the following problem:
I have a rectangular array A to which I want to append
a (probably) one dimensional vector b to make [A|b].
Of course this can be done as np.hstack((x,b[:,None]))
(or obscurely np.r_['1,2,0',x,b]), but this has the following issues:

- what if ``b`` turns out to be a list?
- what if ``b`` turns out to be 2d (e.g., a column vector)?
- it's a bit ugly
- it is not obvious when read by others (e.g., students)

(The last is a key motivation for me to talk about this.)

All of which leads me to wonder if there might be profit
in a numpy.azip function that takes as arguments
- a tuple of arraylike iterables
- an axis along which to concatenate (say, like r_ does) iterated items

To make that a little clearer (but not to provide a suggested implementation),
it might behave something like

def azip(alst, axis=1):
     results = []
     for tpl in zip(*alst):
         results.append(np.r_[tpl])
     return np.rollaxis(np.array(results), axis-1)

Alan Isaac




More information about the NumPy-Discussion mailing list