[SciPy-User] idiom for iterators, expr(T) if isscalar(T) else array([expr(t) for t in T])

Yosef Meller yosefmel@post.tau.ac...
Wed Oct 14 07:02:52 CDT 2009


On יום שני 12 אוקטובר 2009 16:42:21 denis wrote:
> Some vectors are iterators: I want
>     expr(T) if isscalar(T) \
>     else array([expr(t) for t in T])
> Is there an idiom for this ?
> 
> One can
>     try:
>         expr(T)
>     except ValueError:
>     # ValueError: shape mismatch: objects cannot be broadcast to a
> single shape
>         array([ expr(t) for t in T ])
> 
> but this is ugly, and probably wrong in some cases even with no
> ValueError
> I'm afraid the broadcasting rules are just too complex for me /
> I'm too simple for the rules.

v_expr = numpy.vectorize(expr)
v_expr(T)

Is that what you wanted?


More information about the SciPy-User mailing list