Convert List of List of Tuples Into 2d Numpy Array
<p>I have a list of list of tuples:</p>

<pre><code>X = [[(0.5, 0.5, 0.5), (1.0, 1.0, 1.0)], [(0.5, 0.5, 0.52), (1.0, 1.0, 1.0)], [(0.5, 0.52, 0.52), (1.0, 1.0, 1.0)], [(0.52, 0.52, 0.52), (1.0, 1.0, 1.0)], [(0.52, 0.52, 0.52), (1.0, 1.0, 1.0)]]
</code></pre>

<p>and would like to convert it to a 2D numpy array. I tried the following but didn't work. I just want to make sure the new transformed 2D keeps the same shape and structure of X. Please help me , thanks a lot.</p>

<pre><code>import numpy as np
X = [[(0.5, 0.5, 0.5), (1.0, 1.0, 1.0)], [(0.5, 0.5, 0.52), (1.0, 1.0, 1.0)], [(0.5, 0.52, 0.52), (1.0, 1.0, 1.0)], [(0.52, 0.52, 0.52), (1.0, 1.0, 1.0)], [(0.52, 0.52, 0.52), (1.0, 1.0, 1.0)]]
np.asarray([sublist for sublist in X])
</code></pre>

<p>Expected result would be:</p>

<pre><code>[[ 0.5 ,  0.5 ,  0.5 ],
    [ 1.  ,  1.  ,  1.  ]],

   [[ 0.5 ,  0.5 ,  0.52],
    [ 1.  ,  1.  ,  1.  ]],

   [[ 0.5 ,  0.52,  0.52],
    [ 1.  ,  1.  ,  1.  ]],

   [[ 0.52,  0.52,  0.52],
    [ 1.  ,  1.  ,  1.  ]],

   [[ 0.52,  0.52,  0.52],
    [ 1.  ,  1.  ,  1.  ]]
</code></pre>

<p>Dont know if you are working on python 2.x or 3.x but in 3.x you could try:</p>

<pre><code>&gt;&gt; import numpy as np
&gt;&gt; X = [(....)] # Your list
&gt;&gt; array = np.array([*X])
&gt;&gt; print(array)
array([[[ 0.5 ,  0.5 ,  0.5 ],
    [ 1.  ,  1.  ,  1.  ]],

   [[ 0.5 ,  0.5 ,  0.52],
    [ 1.  ,  1.  ,  1.  ]],

   [[ 0.5 ,  0.52,  0.52],
    [ 1.  ,  1.  ,  1.  ]],

   [[ 0.52,  0.52,  0.52],
    [ 1.  ,  1.  ,  1.  ]],

   [[ 0.52,  0.52,  0.52],
    [ 1.  ,  1.  ,  1.  ]]])
</code></pre>

<p>Dont know if this is what you want to achieve.</p>

python numpy