簡體   English   中英

如何將元組列表轉換為 np 數組,其中每個元素都是 python 中的元組

[英]how to transform a list of list of tuples into an np array where each element is a tuple in python

我想在 python 中轉換一個 np 元組數組中的元組列表。

例如,現在我有:

[[(a,b),(b,c),(d,e)]
[(f,g),(h,i),(j,k)]]

我需要 numpy 元組數組:

[[(a,b) (b,c) (d,e)] [(f,g) (h,i) (j,k)]]

形狀為 (2,3)

你可以這樣做,但你對 dtype 非常小心。

In [4]: l = [[(0.0, 1.0), (3.0, 4.1)], [(5.4, 6.6), (7.3, 8.8)] ]                                        

In [5]: np.asarray(l, dtype=np.dtype('f, f'))                                                            
Out[5]: 
array([[(0. , 1. ), (3. , 4.1)],
       [(5.4, 6.6), (7.3, 8.8)]], dtype=[('f0', '<f4'), ('f1', '<f4')])

正如您在np.dtype('f, f')中看到的那樣,元組內只能有兩個元素

您可以在此處閱讀更多信息:結構化 arrays

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM