0

I have an output which is as follows :

data = [array([1.        , 1.14112001, 0.7205845 , 1.41211849, 0.46342708,
        1.65028784, 0.24901275]),
 array([1.83665564, 0.09442164, 1.95637593, 0.01196838, 1.99991186,
        0.00822115, 1.96379539]),
 array([0.08347845, 1.85090352, 0.23174534, 1.67022918, 0.44121095,
        1.43616476])]

That is a list of 3 arrays. I have to flatten the array. This should be simple as data.flatten("F") But it does not work

The output should contain the first element of each array then the second and so on.

something like: 1,1.83665564,1.14112001,0.09442164,1.85090352 and so on. How can i do that

Update :

the output I am getting with given code as follows

array([array([1.        , 1.14112001, 0.7205845 , 1.41211849, 0.46342708,
       1.65028784, 0.24901275]),
       array([1.83665564, 0.09442164, 1.95637593, 0.01196838, 1.99991186,
       0.00822115, 1.96379539]),
       array([0.08347845, 1.85090352, 0.23174534, 1.67022918, 0.44121095,
       1.43616476])], dtype=object)
TheTechGuy
  • 1,568
  • 4
  • 18
  • 45
  • 1
    The arrays are not the same size – Dani Mesejo Jan 07 '19 at 19:03
  • It is not clear to me in what pattern you want to concatenate the indices. Please either add more steps in your example or try to generalize it formally. – Felix Jan 07 '19 at 19:04
  • @DanielMesejo Yes, indeed. That's the problem. I need to add them together. The only way i can do by flatten the array and then add every 3 element – TheTechGuy Jan 07 '19 at 19:04
  • Also in your output you skip 0.08347845? Why? Please add a full output. – Dani Mesejo Jan 07 '19 at 19:06
  • *"But it does not work..."* Please give more details than that. If the code you showed is exactly what you tried, you got an error. Include the complete traceback (i.e. the complete error message) in the question. – Warren Weckesser Jan 07 '19 at 19:06
  • 1
    You say you tried `data.flatten("F")`. But `data` is a Python list, not a numpy array. Python lists do not have a `flatten` method. – Warren Weckesser Jan 07 '19 at 19:07
  • I have converted it into numpy array – TheTechGuy Jan 07 '19 at 19:08
  • Are you looking to get something like this: `[1.0, 1.14112001, 0.7205845, 1.41211849, 0.46342708, 1.65028784, 0.24901275, 1.83665564, 0.09442164, 1.95637593, 0.01196838, 1.99991186, 0.00822115, 1.96379539, 0.08347845, 1.85090352, 0.23174534, 1.67022918, 0.44121095, 1.43616476]`? If so maybe: `[num for long_list in data for num in long_list]`? – hqkhan Jan 07 '19 at 19:08
  • It will be easier for someone to help you if you provide a [minimal, complete and verifiable example](https://stackoverflow.com/help/mcve). – Warren Weckesser Jan 07 '19 at 19:09
  • 2
    `np.hstack(arr)` should work. You have an array of arrays, so the usual `reshape` and `flatten` methods don't work. `hstack` or `concatenate` treat it as a list or sequence of arrays. – hpaulj Jan 07 '19 at 19:09
  • no that i can also get with `list(chain(*data))` . I need to make an array where first element from each array are one after another , then the 2nd element and so on. – TheTechGuy Jan 07 '19 at 19:12
  • 1
    What happens with the array with fewer elements? – Dani Mesejo Jan 07 '19 at 19:12

3 Answers3

1
from numpy import array
data = [array([1.        , 1.14112001, 0.7205845 , 1.41211849, 0.46342708,
        1.65028784, 0.24901275]),
 array([1.83665564, 0.09442164, 1.95637593, 0.01196838, 1.99991186,
        0.00822115, 1.96379539]),
 array([0.08347845, 1.85090352, 0.23174534, 1.67022918, 0.44121095,
        1.43616476])]
max=len(max(data,key=len))
final_list=[]
for index in range (0,max):
        final_list.extend([a[index] for a in data if len(a)>index])
print(final_list)

Output:

[1.0, 1.83665564, 0.08347845, 1.14112001, 0.09442164, 1.85090352, 0.7205845, 1.95637593, 0.23174534, 1.41211849, 0.01196838, 1.67022918, 0.46342708, 1.99991186, 0.44121095, 1.65028784, 0.00822115, 1.43616476, 0.24901275, 1.96379539]
Bitto
  • 7,937
  • 1
  • 16
  • 38
1

Don't know if best solution, but you can use np.resize to reshape all arrays to the same length and stack them vertically. Then use .T to flatten them column based, and use a mask to exclude the values created by np.resize:

l = max(x.size for x in data)
masks = [np.arange(l) < x.size for x in data]
np.vstack(np.resize(x, l) for x in data).T.flatten()[np.vstack(masks).T.flatten()]

>> array([1.        , 1.83665564, 0.08347845, 1.14112001, 0.09442164,
   1.85090352, 0.7205845 , 1.95637593, 0.23174534, 1.41211849,
   0.01196838, 1.67022918, 0.46342708, 1.99991186, 0.44121095,
   1.65028784, 0.00822115, 1.43616476, 0.24901275, 1.96379539])
Tarifazo
  • 4,118
  • 1
  • 9
  • 22
1

Use itertools.zip_longest to get elements in different-length arrays in order (see this post) and then use list comprehension to flatten the resulting tuples.

In Python 2 we should use itertools.izip_longest instead.

>>> from itertools import zip_longest
>>> from numpy import array

# three sample arrays make up a list from 1 to 11
>>> data = array([array([1,4,7,10]), array([2,5,8,11]), array([3,6,9])])
>>> temp = list(zip_longest(*data, fillvalue=None))
>>> temp
[(1, 2, 3), (4, 5, 6), (7, 8, 9), (10, 11, None)]

>>> [i for tup in temp for i in tup if i is not None]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
Louis
  • 766
  • 7
  • 10