1

I have a nested list in python which contains some arrays. I want to check arrays with each other and then remove the rows that do not exist in next array. This is my nested list:

import numpy as np
all_points=[[[np.array([[1., 2.], [3., 4.], [7., 8.], [8., 9.]]), \
              np.array([[20., 30.], [30., 20.], [10., 70.], [70., 40.]])],\
             [np.array([[1., 2.], [3., 4.], [-0.8, -0.9], [7., 8.]]), \
              np.array([[20., 30.], [30., 20.], [10., 70.], [70., 40.], [-70., -40.]])]]]

In my simplified example, it has only one main sublist. Main sublist again has two internal sublists. I want to check the first arrays of the first two internal sublists and only keep the common rows among them. If I check the first arrays of two internal sublists, I see that two rows are not common:

[[8., 9.], [-0.8, -0.9]]

So, I remove them. For the next array of internal sublists, only one row is not common in both of them: [-70., -40.] which should be removed. Then, I want to export the list as the following:

cleaned_points=[[np.array([[1., 2.], [3., 4.], [7., 8.]]), \
                 np.array([[20., 30.], [30., 20.], [10., 70.], [70., 40.]])]]

After doing so, one sublist is removed because I concatenate all the arrays of each internal sublist. In reality I mave have more than one main sublist and two internal sublists. I want to repeat hat I did based on internal sublist in all the existing main sublists. I may have three sublists and then check for common rows between arrays of sublist 2 and 3. I do appreciate any help in advance.

Ali_d
  • 1,089
  • 9
  • 24
  • 1
    Does this answer your question? [Get intersecting rows across two 2D numpy arrays](https://stackoverflow.com/questions/8317022/get-intersecting-rows-across-two-2d-numpy-arrays) – Jérôme Richard Apr 27 '21 at 16:39
  • Dear @ Jérôme Richard, thanks for the link. That was what I want but I don’t know how to call related arrays. I mean first array of first internal sublist and first array of second internal sublist and so on. – Ali_d Apr 27 '21 at 17:00
  • 1
    I am afraid there is nothing better than using loops for that. If you have many small arrays, then you can use Cython or Numba to mitigate a bit the cost of the loops. – Jérôme Richard Apr 27 '21 at 17:04

0 Answers0