You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Is there a recommended way to use numpy.sum() with a chain iterator that returns numpy arrays? Also, is there a reason why array()/sum() work with normal iterators but not with itertools.chain?
Generally, iterators are not supported as arguments to array(). Use fromiter() instead. There isn't a good way to implement array() to take iterators, too. fromiter() makes some assumptions in order to support this for 1D iterators of scalars, but array() cannot make these assumptions.
You can just use the builtin sum() function. array() does not work with normal iterators, just sequences. xrange(), for example, is a sequence, not an iterator. It has __len__ and __getitem__ methods. I don't know exactly what you've tried that leads you to think that normal iterators work.
If you want to talk about it more, please ask on the mailing list. Trac tickets aren't good for back-and-forth conversations.
Original ticket http://projects.scipy.org/numpy/ticket/1005 on 2009-02-11 by trac user rogerbrent, assigned to unknown.
numpy.array(), numpy.sum(), etc. don't seem to work with itertools.chain objects.
e.g.:
from itertools import chain
iter = chain(xrange(5))
array(iter)
returns array(<itertools.chain object at 0x0172C828>, dtype=object).
list(iter) would work as expected, returning [0, 1, 2, 3, 4].
However if I define my own chain() function then array(iter) works fine.
Python v2.5.4 and numpy v1.2.1
The text was updated successfully, but these errors were encountered: