I have a list of numpy arrays and want to split it into chunks based on the length of array. This is my list:
import numpy as np
nums=[[np.array([[1.]]), np.array([[2.]])],\
[np.array([[3.]]), np.array([[5.]])],\
[np.array([[3.], [3.]]), np.array([[8.], [9.]])],\
[np.array([[8.], [9.]]), np.array([[1.], [2.]])],\
[np.array([[5.]]), np.array([[1.]])]]
Each sublist has always two arrays with similar length. First two sublists should be merged because the length of their arrays is the same (1). I take only unique arrays. The third and fourth sublists should be merged because of the same length (2). These two sublists have a common array (np.array([[8.], [9.]])) and I only take unique arrays. Last sublist is also another split. In reality I have several sublist but I want to used the same rule for merging them. Finally, I want to get my list as:
arri=[[np.array([[1.]]), np.array([[2.]]),\
np.array([[3.]]), np.array([[5.]])],\ # first chunk
[np.array([[3.], [3.]]), np.array([[8.], [9.]]),\
np.array([[1.], [2.]])],\ # second chunk
[np.array([[5.]]), np.array([[1.]])]] # third chunk
I tried the following code but it was not successful:
arri=[]
for i in range (len (nums)-1):
if (len(nums[i][0]) == len(nums[i+1][0]) and
any((nums[i+1][0] == x).all() for x in nums[i])):
arri.append ([nums[i], nums[i+1]])
In advance, I do appreciate any help.
arriin my question. It has three sublists. First sublist is created by merging unique arrays of first two sublists of input. Second sublist ofarriis created by merging unique arrays of third and fourth sublists of input. Last one is also simply the last sublist of input. The problem with my try is that it cannot export the last sublist and still I cannot get unique arrays.