1

What I have is:

x = array([a, a, a, a, a, a],
          [b, b, b, b, b, b],
          [................],
          [z,z,z,z,z,z,z,z,])

I am looking for a way about:

i. How to covert this array into a 1D array x = array[a,a,a,a,a,a,a,a,b,b,b,b,b,..,z,z,z] and a 1D list x = [a,a,a,a,a,a,a,b,b,b,b,b,...,z,z,z].

ii. Calculating the exact dimensions of this array: I tried len(x[0]) and len(x) to get the both dimensional coordinates, but this way seems a bit hackish. Is there any built-in function to get the dimensions of an array?

Note: some of the array values are positive integers, some of them are floats, some of them are negative floats, and some of them are negative integers.

Many thanks in advance.

4 Answers 4

2

I use np.ravel for this task.
Just call the list constructor if that's how you want output.
.shape will give you the number of rows and columns, respectively, but you don't actually need to know them.

>>> import numpy as np
>>> a = np.random.random_integers(0, 9, (3, 4))
>>> a.shape
(3, 4)
>>> print a
[[9 5 1 8]
 [3 1 3 1]
 [3 9 6 6]]
>>> a.ravel()
array([9, 5, 1, 8, 3, 1, 3, 1, 3, 9, 6, 6])
>>> list(a.ravel())
[9, 5, 1, 8, 3, 1, 3, 1, 3, 9, 6, 6]
Sign up to request clarification or add additional context in comments.

2 Comments

This works! fine. All I had to do is to have some modifications with the files and apply the np.ravel(). I am done. Thanks a million. :-)
There is also .flat, which is just an iterator but enough for such purpose. The advantage is that .ravel() may have to create a copy of the data. It doesn't matter for you I am sure, but it can be a nice feature.
1

To create a 1D array from an ND array, use ndarray.flatten()

x = array([a, a, a, a, a, a],
      [b, b, b, b, b, b],
      [................],
      [z,z,z,z,z,z,z,z,])
x.flatten()

To get a list from this, use ndarray.tolist()

xlist = x.tolist()

To get the dimensions of an array, use ndarray.shape

print x.shape
(4, 6)

2 Comments

for 1D list, you probably want x.flatten().tolist()
working with flatten simply allocates every tuple into a single array. I need a single array like x = [a,a,a,a...z,z,z,z] which lists everything from that multi-dimensional array into a serialized format. :-(
1
  • To transform the 2D array as a 1D array, you can use np.ravel(a). If you want to do so in place, you can also use a.shape=-1. The a.flatten method will return a copy of the initial array, which is wasteful.
  • To transform your 1D array into a list, you can use list(a) or a.tolist(). To transform your 2D array into a flat list, the simpler by far is list(a.flat) (.flat returns a 'flat' iterator). Note that using a.tolist on a 2D array will give you a list of list, while using list(a) will give you a list of arrays.
  • Use a.shape to get the sizes of a along each dimension, as a tuple with as many elements as dimensions. Note that len(a)==a.shape[0]. With your 2D array, len(a[0])=a.shape[1].

Comments

0

Merge arrays into a new array :

newArray = array()
for y in x
  newArray += y

find length :

len(newArray)

2 Comments

To create a 1D array use ndarray.flatten()
gabitzish and Vorticity: both of the methods are not working. I want to get a single array like array[a,a,a,a,...z,z,z,z] without any objects or tuples in them. Just a serialized list. It's not happening with the methods you guys specified.

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.