How to make a Structured Array from multiple simple array

后端 未结 2 1714
花落未央
花落未央 2021-01-16 02:10
import numpy as np

a=np.array([1,2,3,4,5,6,7,8,9])
b=np.array([\"a\",\"b\",\"c\",\"d\",\"e\",\"f\",\"g\",\"h\",\"i\"])
c=np.array([9,8,7,6,5,4,3,2,1])
datatype=np.d         


        
2条回答
  •  南笙
    南笙 (楼主)
    2021-01-16 02:19

    You might as well try numpy.rec.fromarrays.

    import numpy as np
    
    a=np.array([1,2,3,4,5,6,7,8,9])
    b=np.array(["a","b","c","d","e","f","g","h","i"])
    c=np.array([9,8,7,6,5,4,3,2,1])
    
    d = np.rec.fromarrays([a,b,c], formats=['i','S32','i'], names=['num','char','len'])
    

    Although timings are not as good as using itertools.

    In [2]: %timeit d = np.rec.fromarrays([a,b,c], formats=['i','S32','i'], names=['num','char','len'])
    10000 loops, best of 3: 86.5 us per loop
    
    In [6]: import itertools
    
    In [7]: %timeit np.fromiter(itertools.izip(a,b,c),dtype=datatype)
    100000 loops, best of 3: 11.5 us per loop
    

提交回复
热议问题