Question

So I have a array of arrays, like

[array([-0.05504106,  4.21890792]), array([-0.05504106,  4.21890792]), array([-0.0533802 ,  4.10717668]), array([-0.0546635 ,  4.19501313])]

And what I'm attempting to do is make it into an array of 2 arrays, in which the components are put together like this:

[array([-0.05504106, -0.05504106, -.0533802, -.0546635]), array([4.21890792, 4.21890792, 4.10717668, 4.19501313])

I've tried a number of approaches. The first is as follows:

for i in range(len(array_1)-1):
zipped = zip(array_1[i], array_1[i+1], array_1[i+2])
print zipped

However, this approach has the drawback of having to add an array_1[i+n] for each additional array within array_1. Not at all practical if array_1 has many arrays within it.

The next thing I tried was attempting to use itertools.repeat in conjunction with the above code, like so:

for i in range(len(array_1)-1):
zipped = zip(itertools.repeat(array_1[i], len(array_1))
print zipped

however, this did not function the way I wanted it to.

Could you give me some idea of how I would accomplish this task? Should I be using zip and/or intertools.repeat?

Was it helpful?

Solution

I am not sure which array class you are using, but with simple lists you can use zip:

a = [[-0.05504106, 4.21890792], [-0.05504106, 4.21890792], [-0.0533802, 4.10717668],[-0.0546635, 4.19501313]]

b = list(zip(*a)) #omit list in python2
print(b)

This prints:

[(-0.05504106, -0.05504106, -0.0533802, -0.0546635), (4.21890792, 4.21890792, 4.10717668, 4.19501313)]
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top