2017-11-20 12 views
1

私はこのようなリストがあります:私はnumpyの配列に変換し、タプルとして配列項目の種類を維持したいタプのリストをnumpyのタプルの配列に変換するにはどうすればよいですか?

l=[(1,2),(3,4)] 

array([(1,2),(3,4)]) 

をしかしnumpy.array(l)が得られます:

array([[1,2],[3,4)]]) 

アイテムタイプはnumpy.ndarrayするタプルから変更された、私はアイテムタイプを指定

numpy.array(l,numpy.dtype('float,float')) 

これは与える:

array([(1,2),(3,4)]) 

が、項目タイプはタプルではなく、numpy.voidは、その質問は:

how to convert it to a numpy.array of tuple,not of numpy.void? 

答えて

1

あなたは各要素をさせる、object DTYPEの配列を持つことができます

out = np.empty(len(l), dtype=object) 
out[:] = l 

サンプルラン - - 配列の組、そうようなもの

In [163]: l = [(1,2),(3,4)] 

In [164]: out = np.empty(len(l), dtype=object) 

In [165]: out[:] = l 

In [172]: out 
Out[172]: array([(1, 2), (3, 4)], dtype=object) 

In [173]: out[0] 
Out[173]: (1, 2) 

In [174]: type(out[0]) 
Out[174]: tuple 
関連する問題