6

I have a list of list of tuples:

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)]]

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.

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])

Expected result would be:

[[ 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.  ]]
7
  • 2
    Your data can't be converted to a 2D array as it is. Whats your expected result? Commented Oct 1, 2016 at 11:49
  • 1
    "It did not work" is not a problem description. Commented Oct 1, 2016 at 11:49
  • The current structure of X lends itself to an array of shape (5,2,3). There are then a number of ways that one could turn this into a 2D array, so please specify what your expected result is. Commented Oct 1, 2016 at 12:02
  • That Expected result is not one array. Commented Oct 1, 2016 at 12:14
  • 1
    5 arrays. To have one array, you should have one starting bracket at the start of the array and a matching ending bracket at the end of the array definition. You should look into how multi-dim arrays are stored. Commented Oct 1, 2016 at 12:22

2 Answers 2

7

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

>> import numpy as np
>> X = [(....)] # Your list
>> array = np.array([*X])
>> 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.  ]]])

Dont know if this is what you want to achieve.

Sign up to request clarification or add additional context in comments.

2 Comments

your not using my input, please refer back to my input. Also the expected output should be 2D array not 3D which what is my question is about!!!
First of all: There is a comment on the 2nd line, read it. 2nd: How do you expect to get ONE 2D array, when your results are 5 2D arrays? If all you want is FIVE 2D arrays, just use: arrays = [np.asarray(sublist) for sublist in X]
2

If I do the normal thing to your list I get a 3d array:

In [38]: np.array(X)
Out[38]: 
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.  ]]])
In [39]: _.shape
Out[39]: (5, 2, 3)

The print (str) display looks a lot like your desired result - except for the extra set of [] to enclose the whole thing:

In [40]: print(__)
[[[ 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.  ]]]

I could split it into a list of 5 2d arrays, but the display is rather different:

In [43]: np.split(np.array(X),5)
Out[43]: 
[array([[[ 0.5,  0.5,  0.5],
         [ 1. ,  1. ,  1. ]]]), array([[[ 0.5 ,  0.5 ,  0.52],
         [ 1.  ,  1.  ,  1.  ]]]), array([[[ 0.5 ,  0.52,  0.52],
        ...]

A list comprehension turning each sublist into an array does the same thing, [np.array(x) for x in X]

I could also reshape the (5,2,3) array into 2d arrays, (10,3) or (5,6) for example, but they won't look like your target.

Is the tuple layer important (as opposed to being a list)? I could preserve it (appearance wise) with a structured array: np.array(X, dtype=('f,f,f')).

1 Comment

TIL you can use __ and ___ to access the second and third last return value in an interactive shell.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.