[Numpy-discussion] fromiter + dtype='S' -> Python crash

Alan G Isaac aisaac at american.edu
Thu Mar 13 17:44:54 EDT 2008


On Thu, 13 Mar 2008, Alexander Michael apparently wrote:
>  I wasn't sure if there was a magic numpy
> method to do the loop quickly (as the destination array is created 
> beforehand) without creating a temporary Python list, but I guess not. 
> The generator/list-comprehension is likely better than my prototype. 


Looks like I misunderstood your question:
you want an **array** of strings?
In principle you should be able to use ``fromiter``,
I believe, but it does not work.  BUG? (Crasher.)

Cheers,
Alan Isaac


>>> import numpy as N
>>> x = [1,2,3]
>>> fmt="%03d"
>>> N.array([fmt%xi for xi in x],dtype='S')
array(['001', '002', '003'],
      dtype='|S3')
>>> N.fromiter([xi for xi in x],dtype='float')
array([ 1.,  2.,  3.])
>>> N.fromiter([xi for xi in x],dtype='S')
Python crashes.





More information about the NumPy-Discussion mailing list