0

Let's say I have a nested array like so:

array = [[1,7], [49, 9], [3, 80], [13, 15]]

From the above array, I want to create two new arrays (within a nested array) based on vertical "columns". This is my desired output:

new_array = [[1, 49, 3, 13], [7, 9, 80, 15]]

I want the logic to work weather I have individually nested arrays of length 2 (as in my example) or 30 (or more).

I tried using a dictionary to do this. The logic of the code looked something like this but the dictionary is not producing what I'm looking for:

featureCol["valuesList{}".format(i)].append(nestedArray[i])   

Also, the only library I can use is numpy. I don't have any additional libraries available to me in Python.

BrokenBenchmark
  • 18,126
  • 7
  • 21
  • 33
PineNuts0
  • 4,740
  • 21
  • 67
  • 112

1 Answers1

1

You can use zip() with list unpacking. This operation assumes that all of the input lists are of the same length, but it does not make any assumptions on the sizes of the input lists otherwise:

result = list(list(item) for item in zip(*array))
print(result)

This outputs:

[[1, 49, 3, 13], [7, 9, 80, 15]]
BrokenBenchmark
  • 18,126
  • 7
  • 21
  • 33