найти равных numpy ndarrays в списке списков - PullRequest
0 голосов
/ 21 апреля 2020

У меня есть список m (вставленный в конце), состоящий из numpy массивов. Форма:

>> np.shape(m)
(23, 2)

После достижения 2-й вложенной глубины число массивов в списке списка изменяется между 1 или 2, например,

здесь есть один массив:

>> m[2][1]                                                                       

[array([  6,   7,   9,  24,  35,  44,  60,  61,  62,  63,  66,  67,  96,
         97, 115, 124, 132, 133, 149, 150, 163, 164, 165, 166, 167, 168,
        169, 180, 197, 217, 218, 220])]

или два из них здесь:

>> m[2][0]  

[array([  6,   7,   9,  24,  35,  44,  60,  61,  62,  63,  66,  67,  96,
         97, 115, 124, 132, 133, 149, 163, 164, 165, 166, 167, 168, 169,
        180]),
 array([150, 197, 217, 218, 220])]

Что бы я хотел сделать:

Если бы я oop превысил i, j и k следующим образом:

>>> for i in range(np.shape(m)[0]): # outer loop, from i=0 to i=22
    for j in range(np.shape(m)[1]): # 1st nested loop, from j=0 to j=1
        for k in range(np.shape(m[i][j])[0]): # 2nd nested loop, from k=0 to k=1, or just k=0
            print ("i = ", i, "j = ", j, "k = ", k, "components = " ,m[i][j][k])

i =  0 j =  0 k =  0 components =  [  1  14  15  16  39  40  50  51  76  77  78  79  80  81 119 120 136 143
 144 145 162 176 177 178 199 200 201 202 203 204 205 215] 
### Are all of m[0][0][0] components seen further on in the m array?

i =  0 j =  1 k =  0 components =  [  1  14  15  16  39  40  50  51  76  77  78  79  80  81 119 120 136 143
 162 176 177 178 215]
### Are all of m[0][1][0] components seen further on in the m array?

Как только мы достигли l oop сверх k, у нас есть уникальный список компонентов.

Я хотел бы получить i, j и k, где этот список компонентов повторяется далее в списке m.

Например, компоненты i=1, j=1, k=1 снова отображаются в i=2, j=0, k=0

>>> m[1][1][1]       
array([150, 197, 217, 218, 220])

>>> m[2][0][0]
array([150, 197, 217, 218, 220])

И сохраните эту информацию в списке: t = [[1, 1, 1], [2, 0, 0]]

Что мне удалось сделать до сих пор:

Как только я достиг самый глубокий внутренний l oop сверх k, я думаю сделать это:

for i in range(np.shape(m)[0]): # outer loop, from i=0 to i=22
    for j in range(np.shape(m)[1]): # 1st nested loop, from j=0 to j=1
        for k in range(np.shape(m[i][j])[0]): # 2nd nested loop, from k=0 to k=1, or just k=0
            print ("i = ", i, "j = ", j, "k = ", k, "components = " ,m[i][j][k]) 
            list_search = m[i][j][k] 

            print ('list_search = ', list_search) 
            if np.all(list_search == m[i][j][k]): 
                print ("match: i = ", i, "; j= ", j, "; k = ", k) 

        if np.all(list_search == m[i][j][k]): 
            print ("match: i = ", i, "; j= ", j, "; k = ", k) 


    if np.all(list_search == m[i][j][k]): 
        print ("match: i = ", i, "; j= ", j, "; k = ", k) 

Расположение этих операторов if np.all, кажется, не правильно, так как это приводит к нескольким совпадениям там, где они должны не быть Мне трудно понять, что не так.

m = [([np.array([  1,  14,  15,  16,  39,  40,  50,  51,  76,  77,  78,  79,  80,
           81, 119, 120, 136, 143, 144, 145, 162, 176, 177, 178, 199, 200,
          201, 202, 203, 204, 205, 215])],
  [np.array([  1,  14,  15,  16,  39,  40,  50,  51,  76,  77,  78,  79,  80,
           81, 119, 120, 136, 143, 162, 176, 177, 178, 215]),
   np.array([144, 145, 199, 200, 201, 202, 203, 204, 205])]),
 ([np.array([  6,   7,   9,  24,  35,  44,  60,  61,  62,  63,  66,  67,  96,
           97, 115, 124, 132, 133, 149, 150, 163, 164, 165, 166, 167, 168,
          169, 180, 197, 217, 218, 220])],
  [np.array([  6,   7,   9,  24,  35,  44,  60,  61,  62,  63,  66,  67,  96,
           97, 115, 124, 132, 133, 149, 163, 164, 165, 166, 167, 168, 169,
          180]),
   np.array([150, 197, 217, 218, 220])]),
 ([np.array([  6,   7,   9,  24,  35,  44,  60,  61,  62,  63,  66,  67,  96,
           97, 115, 124, 132, 133, 149, 163, 164, 165, 166, 167, 168, 169,
          180]),
   np.array([150, 197, 217, 218, 220])],
  [np.array([  6,   7,   9,  24,  35,  44,  60,  61,  62,  63,  66,  67,  96,
           97, 115, 124, 132, 133, 149, 150, 163, 164, 165, 166, 167, 168,
          169, 180, 197, 217, 218, 220])]),
 ([np.array([  6,   7,   9,  24,  35,  44,  60,  61,  62,  63,  66,  67,  96,
           97, 115, 124, 132, 133, 149, 150, 163, 164, 165, 166, 167, 168,
          169, 180, 197, 217, 218, 220])],
  [np.array([132, 133, 163, 164, 165, 166, 167, 168, 169]),
   np.array([  6,   7,   9,  24,  35,  44,  60,  61,  62,  63,  66,  67,  96,
           97, 115, 124, 149, 150, 180, 197, 217, 218, 220])]),
 ([np.array([132, 133, 163, 164, 165, 166, 167, 168, 169]),
   np.array([  6,   7,   9,  24,  35,  44,  60,  61,  62,  63,  66,  67,  96,
           97, 115, 124, 149, 150, 180, 197, 217, 218, 220])],
  [np.array([  6,   7,   9,  24,  35,  44,  60,  61,  62,  63,  66,  67,  96,
           97, 115, 124, 132, 133, 149, 150, 163, 164, 165, 166, 167, 168,
          169, 180, 197, 217, 218, 220])]),
 ([np.array([  6,   7,   9,  24,  35,  44,  60,  61,  62,  63,  66,  67,  96,
           97, 115, 124, 132, 133, 149, 150, 163, 164, 165, 166, 167, 168,
          169, 180, 197, 217, 218, 220])],
  [np.array([132, 133, 163, 164, 165, 166, 167, 168, 169]),
   np.array([  6,   7,   9,  24,  35,  44,  60,  61,  62,  63,  66,  67,  96,
           97, 115, 124, 149, 150, 180, 197, 217, 218, 220])]),
 ([np.array([132, 133, 163, 164, 165, 166, 167, 168, 169]),
   np.array([  6,   7,   9,  24,  35,  44,  60,  61,  62,  63,  66,  67,  96,
           97, 115, 124, 149, 150, 180, 197, 217, 218, 220])],
  [np.array([  6,   7,   9,  24,  35,  44,  60,  61,  62,  63,  66,  67,  96,
           97, 115, 124, 132, 133, 149, 150, 163, 164, 165, 166, 167, 168,
          169, 180, 197, 217, 218, 220])]),
 ([np.array([129, 130, 154, 155, 156, 157, 158, 159, 160]),
   np.array([  6,   7,   9,  24,  35,  44,  60,  61,  62,  63,  66,  67,  96,
           97, 115, 124, 132, 133, 149, 150, 163, 164, 165, 166, 167, 168,
          169, 180, 197, 217, 218, 220])],
  [np.array([  6,   7,   9,  24,  35,  44,  60,  61,  62,  63,  66,  67,  96,
           97, 115, 124, 129, 130, 132, 133, 149, 150, 154, 155, 156, 157,
          158, 159, 160, 163, 164, 165, 166, 167, 168, 169, 180, 197, 217,
          218, 220])]),
 ([np.array([  6,   7,   9,  24,  35,  44,  60,  61,  62,  63,  66,  67,  96,
           97, 115, 124, 129, 130, 132, 133, 149, 150, 154, 155, 156, 157,
          158, 159, 160, 163, 164, 165, 166, 167, 168, 169, 180, 197, 217,
          218, 220])],
  [np.array([129, 130, 154, 155, 156, 157, 158, 159, 160]),
   np.array([  6,   7,   9,  24,  35,  44,  60,  61,  62,  63,  66,  67,  96,
           97, 115, 124, 132, 133, 149, 150, 163, 164, 165, 166, 167, 168,
          169, 180, 197, 217, 218, 220])]),
 ([np.array([129, 130, 154, 155, 156, 157, 158, 159, 160]),
   np.array([  6,   7,   9,  24,  35,  44,  60,  61,  62,  63,  66,  67,  96,
           97, 115, 124, 132, 133, 149, 150, 163, 164, 165, 166, 167, 168,
          169, 180, 197, 217, 218, 220])],
  [np.array([  6,   7,   9,  24,  35,  44,  60,  61,  62,  63,  66,  67,  96,
           97, 115, 124, 129, 130, 132, 133, 149, 150, 154, 155, 156, 157,
          158, 159, 160, 163, 164, 165, 166, 167, 168, 169, 180, 197, 217,
          218, 220])]),
 ([np.array([  6,   7,   9,  24,  35,  44,  60,  61,  62,  63,  66,  67,  96,
           97, 115, 124, 129, 130, 132, 133, 149, 150, 154, 155, 156, 157,
          158, 159, 160, 163, 164, 165, 166, 167, 168, 169, 180, 197, 217,
          218, 220])],
  [np.array([129, 130, 154, 155, 156, 157, 158, 159, 160]),
   np.array([  6,   7,   9,  24,  35,  44,  60,  61,  62,  63,  66,  67,  96,
           97, 115, 124, 132, 133, 149, 150, 163, 164, 165, 166, 167, 168,
          169, 180, 197, 217, 218, 220])]),
 ([np.array([129, 130, 154, 155, 156, 157, 158, 159, 160]),
   np.array([  6,   7,   9,  24,  35,  44,  60,  61,  62,  63,  66,  67,  96,
           97, 115, 124, 132, 133, 149, 150, 163, 164, 165, 166, 167, 168,
          169, 180, 197, 217, 218, 220])],
  [np.array([  6,   7,   9,  24,  35,  44,  60,  61,  62,  63,  66,  67,  96,
           97, 115, 124, 129, 130, 132, 133, 149, 150, 154, 155, 156, 157,
          158, 159, 160, 163, 164, 165, 166, 167, 168, 169, 180, 197, 217,
          218, 220])]),
 ([np.array([  6,   7,   9,  24,  35,  44,  60,  61,  62,  63,  66,  67,  96,
           97, 115, 124, 129, 130, 132, 133, 149, 150, 154, 155, 156, 157,
          158, 159, 160, 163, 164, 165, 166, 167, 168, 169, 180, 197, 217,
          218, 220])],
  [np.array([129, 130, 132, 154, 155, 156, 157, 158, 159, 160, 163, 164, 166]),
   np.array([  6,   7,   9,  24,  35,  44,  60,  61,  62,  63,  66,  67,  96,
           97, 115, 124, 133, 149, 150, 165, 167, 168, 169, 180, 197, 217,
          218, 220])]),
 ([np.array([  6,   7,   9,  24,  35,  44,  60,  61,  62,  63,  66,  67,  96,
           97, 115, 124, 133, 149, 150, 165, 167, 168, 169, 180, 197, 217,
          218, 220])],
  [np.array([  6,   7,  35,  60,  61,  62,  63, 115, 133, 149, 150, 165, 167,
          168, 169, 180, 197, 217, 218, 220])]),
 ([np.array([  6,   7,  35,  60,  61,  62,  63, 115, 133, 149, 150, 165, 167,
          168, 169, 180, 197, 217, 218, 220])],
  [np.array([  6,   7,   9,  24,  35,  44,  60,  61,  62,  63,  66,  67,  96,
           97, 115, 124, 133, 149, 150, 165, 167, 168, 169, 180, 197, 217,
          218, 220])]),
 ([np.array([  8,  22,  23,  25,  36,  43,  64,  65,  92,  93,  94,  95,  98,
           99, 116, 123, 137, 161, 216]),
   np.array([144, 145, 199, 200, 201, 202, 203, 204, 205])],
  [np.array([  8,  22,  23,  25,  36,  43,  64,  65,  92,  93,  94,  95,  98,
           99, 116, 123, 137, 144, 145, 161, 199, 200, 201, 202, 203, 204,
          205, 216])]),
 ([np.array([  6,   7,   9,  24,  35,  44,  60,  61,  62,  63,  66,  67,  96,
           97, 115, 124, 133, 149, 150, 165, 167, 168, 169, 180, 197, 217,
          218, 220])],
  [np.array([133, 165, 167, 168, 169]),
   np.array([  6,   7,   9,  24,  35,  44,  60,  61,  62,  63,  66,  67,  96,
           97, 115, 124, 149, 150, 180, 197, 217, 218, 220])]),
 ([np.array([133, 165, 167, 168, 169]),
   np.array([  6,   7,   9,  24,  35,  44,  60,  61,  62,  63,  66,  67,  96,
           97, 115, 124, 149, 150, 180, 197, 217, 218, 220])],
  [np.array([  6,   7,   9,  24,  35,  44,  60,  61,  62,  63,  66,  67,  96,
           97, 115, 124, 133, 149, 150, 165, 167, 168, 169, 180, 197, 217,
          218, 220])]),
 ([np.array([  8,  22,  23,  25,  36,  43,  64,  65,  92,  93,  94,  95,  98,
           99, 116, 123, 137, 144, 145, 161, 199, 200, 201, 202, 203, 204,
          205, 216])],
  [np.array([  8,  22,  23,  25,  36,  43,  64,  65,  92,  93,  94,  95,  98,
           99, 116, 123, 137, 161, 216]),
   np.array([144, 145, 199, 200, 201, 202, 203, 204, 205])]),
 ([np.array([129, 130, 132, 154, 155, 156, 157, 158, 159, 160, 163, 164, 166]),
   np.array([  6,   7,   9,  24,  35,  44,  60,  61,  62,  63,  66,  67,  96,
           97, 115, 124, 133, 149, 150, 165, 167, 168, 169, 180, 197, 217,
          218, 220])],
  [np.array([  6,   7,   9,  24,  35,  44,  60,  61,  62,  63,  66,  67,  96,
           97, 115, 124, 129, 130, 132, 133, 149, 150, 154, 155, 156, 157,
          158, 159, 160, 163, 164, 165, 166, 167, 168, 169, 180, 197, 217,
          218, 220])]),
 ([np.array([  6,   7,   9,  24,  35,  44,  60,  61,  62,  63,  66,  67,  96,
           97, 115, 124, 129, 130, 132, 133, 149, 150, 154, 155, 156, 157,
          158, 159, 160, 163, 164, 165, 166, 167, 168, 169, 180, 197, 217,
          218, 220])],
  [np.array([129, 130, 132, 133, 154, 155, 156, 157, 158, 159, 160, 163, 164,
          165, 166, 167, 168, 169]),
   np.array([  6,   7,   9,  24,  35,  44,  60,  61,  62,  63,  66,  67,  96,
           97, 115, 124, 149, 150, 180, 197, 217, 218, 220])]),
 ([np.array([129, 130, 132, 133, 154, 155, 156, 157, 158, 159, 160, 163, 164,
          165, 166, 167, 168, 169])],
  [np.array([129, 130, 132, 154, 155, 156, 157, 158, 159, 160, 163, 164, 166]),
   np.array([133, 165, 167, 168, 169])]),
 ([np.array([133, 165, 167, 168, 169]),
   np.array([  6,   7,   9,  24,  35,  44,  60,  61,  62,  63,  66,  67,  96,
           97, 115, 124, 149, 150, 180, 197, 217, 218, 220])],
  [np.array([  6,   7,   9,  24,  35,  44,  60,  61,  62,  63,  66,  67,  96,
           97, 115, 124, 133, 149, 150, 165, 167, 168, 169, 180, 197, 217,
          218, 220])])]
...