[Numpy-discussion] Tuple outer product?
josef.pktd@gmai...
josef.pktd@gmai...
Fri Sep 25 13:59:10 CDT 2009
On Fri, Sep 25, 2009 at 2:53 PM, Keith Goodman <kwgoodman@gmail.com> wrote:
> On Fri, Sep 25, 2009 at 11:05 AM, Mads Ipsen <mpi@comxnet.dk> wrote:
>> Sure, and you could do the same thing using two nested for loops. But
>> its bound to be slow when the arrays become large (and they will). The
>> question is whether it can be done in pure numpy. An output like
>>
>> [[[1,4],[1,5],[1,6]],[[2,4],[2,5],[2,6]],[[3,4],[3,5],[3,6]]]
>>
>> is also OK.
>
> This doesn't work. But maybe someone can make it work:
>
>>> x = np.array([1,2,3])
>>> y = np.array([4,5,6])
>>>
>>> a = np.ones((3,3,2), dtype=np.int)
>>> a[:,:,0] = x
>>> a[:,:,1] = y
>>> a.tolist()
> [[[1, 4], [2, 5], [3, 6]], [[1, 4], [2, 5], [3, 6]], [[1, 4], [2,
> 5], [3, 6]]]
and if you really want tuples something like this might work
>>> np.dstack(np.meshgrid([1,2,3],[4,5,6])).T.reshape(2,-1).T.copy().view([('',np.int32)]*2).reshape(3,-1).tolist()
[[(1, 4), (1, 5), (1, 6)], [(2, 4), (2, 5), (2, 6)], [(3, 4), (3, 5), (3, 6)]]
by trial and error, I'm not sure if every step is necessary and works in general
Josef
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion@scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion
>
More information about the NumPy-Discussion
mailing list