Home > Back-end >  Merge/Combine multi-dimensions from a list to a new array with Python
Merge/Combine multi-dimensions from a list to a new array with Python

Time:07-07

I would like to merge/combine each item in a list, which has multi-dimensions, into a new array. Below is my code and expectation.

Example of 2D dimensions:

Input: [[1, 2], [3, 4], [5, 6]], [[7, 8], [9, 10], [11, 12]]

Output: [[1, 2], [3, 4], [5, 6], [7, 8], [9, 10], [11, 12]]

However, the dimensions are dynamic, they can be [1, 2, ...]

import numpy as np

regular_list = []
regular_list.append([[[-0.1905   , -0.4675   ],[-0.573    , -0.8115   ]], [[-0.468    , -0.3585   ],[-0.8145   , -0.7735   ]], [[-0.356    , -0.1435   ],[-0.7645   , -0.5415   ]]])
regular_list.append([[[-0.145     , 0.52      ],[-0.065     , 0.195     ]], [[-0.535     , 0.405     ],[-0.05      , 0.285     ]], [[-0.39      , 0.375     ],[-0.275     , 0.26      ]]])
regular_list.append([[[-0.2445    , 0.1275    ],[-0.229    , -0.147    ]], [[-0.117    , -0.043    ],[-0.151    , -0.046    ]], [[0.03      , 0.0245    ],[0.047    , 0.049    ]]])
  
flat_list = [i for i in regular_list]

print('Original list', regular_list)
print('Transformed list', flat_list)

Current:

[[[[-0.1905, -0.4675], [-0.573, -0.8115]], [[-0.468, -0.3585], [-0.8145, -0.7735]], [[-0.356, -0.1435], [-0.7645, -0.5415]]],
 [[[-0.145, 0.52], [-0.065, 0.195]], [[-0.535, 0.405], [-0.05, 0.285]], [[-0.39, 0.375], [-0.275, 0.26]]],
 [[[-0.2445, 0.1275], [-0.229, -0.147]], [[-0.117, -0.043], [-0.151, -0.046]], [[0.03, 0.0245], [0.047, 0.049]]]]

Expectation:

[[[[-0.1905, -0.4675], [-0.573, -0.8115]], [[-0.468, -0.3585], [-0.8145, -0.7735]], [[-0.356, -0.1435], [-0.7645, -0.5415]]],
 [[-0.145, 0.52], [-0.065, 0.195]], [[-0.535, 0.405], [-0.05, 0.285]], [[-0.39, 0.375], [-0.275, 0.26]],
 [[-0.2445, 0.1275], [-0.229, -0.147]], [[-0.117, -0.043], [-0.151, -0.046]], [[0.03, 0.0245], [0.047, 0.049]]]

We can try here: https://trinket.io/python3/b87762f44f

CodePudding user response:

I edited your source code and solved it using recursion:

def merge(arr):
  res = []
  for x in arr:
    if isinstance(x[0], list):
      res = res   merge(x)
    else:
      res.append(x)
  return res

regular_list = []
regular_list.append([[[-0.1905   , -0.4675   ],[-0.573    , -0.8115   ]], [[-0.468    , -0.3585   ],[-0.8145   , -0.7735   ]], [[-0.356    , -0.1435   ],[-0.7645   , -0.5415   ]]])
regular_list.append([[[-0.145     , 0.52      ],[-0.065     , 0.195     ]], [[-0.535     , 0.405     ],[-0.05      , 0.285     ]], [[-0.39      , 0.375     ],[-0.275     , 0.26      ]]])
regular_list.append([[[-0.2445    , 0.1275    ],[-0.229    , -0.147    ]], [[-0.117    , -0.043    ],[-0.151    , -0.046    ]], [[0.03      , 0.0245    ],[0.047    , 0.049    ]]])


print('Original list', regular_list)
print('Transformed list', merge(regular_list))

Maybe it's different from your expectations, but I think it meets the requirements:

[[-0.1905, -0.4675], [-0.573, -0.8115], [-0.468, -0.3585], [-0.8145, -0.7735], [-0.356, -0.1435], [-0.7645, -0.5415], [-0.145, 0.52], [-0.065, 0.195], [-0.535, 0.405], [-0.05, 0.285], [-0.39, 0.375], [-0.275, 0.26], [-0.2445, 0.1275], [-0.229, -0.147], [-0.117, -0.043], [-0.151, -0.046], [0.03, 0.0245], [0.047, 0.049]]

Make sure the smallest unit is an array with only 2 elements. Or you can improve it according to your purpose.

CodePudding user response:

def flat_func(regular_list):
    res = []
    for idx,item in enumerate(regular_list):
        print(idx, item)
        if idx==0:
            res.append(item)
        else:
            for i in item:
                res.append(i)
    return res
regular_list = []
regular_list.append([[[-0.1905   , -0.4675   ],[-0.573    , -0.8115   ]], [[-0.468    , -0.3585   ],[-0.8145   , -0.7735   ]], [[-0.356    , -0.1435   ],[-0.7645   , -0.5415   ]]])
regular_list.append([[[-0.145     , 0.52      ],[-0.065     , 0.195     ]], [[-0.535     , 0.405     ],[-0.05      , 0.285     ]], [[-0.39      , 0.375     ],[-0.275     , 0.26      ]]])
regular_list.append([[[-0.2445    , 0.1275    ],[-0.229    , -0.147    ]], [[-0.117    , -0.043    ],[-0.151    , -0.046    ]], [[0.03      , 0.0245    ],[0.047    , 0.049    ]]])
  
expectation_list = [[[[-0.1905, -0.4675], [-0.573, -0.8115]], [[-0.468, -0.3585], [-0.8145, -0.7735]], [[-0.356, -0.1435], [-0.7645, -0.5415]]],
 [[-0.145, 0.52], [-0.065, 0.195]], [[-0.535, 0.405], [-0.05, 0.285]], [[-0.39, 0.375], [-0.275, 0.26]],
 [[-0.2445, 0.1275], [-0.229, -0.147]], [[-0.117, -0.043], [-0.151, -0.046]], [[0.03, 0.0245], [0.047, 0.049]]]

print('Original list', regular_list)
print(expectation_list==flat_func(regular_list)) 
#True
  • Related