code: def move_groups_to_match(B, A, debug=False):
      Ag = make_grouped(A)
      Bg = make_grouped(B)
  
      new_Bg = [[] for g in range(len(Ag))]
      wrong_entry_dict = dictify(group_differences(A, B))
  
      if debug:
          print(
              f"MGTM:\n\tAg: {Ag}\n\tBg: {Bg}\n\tWED: {wrong_entry_dict}"
          )
  
      moved_groups = []
  
      keys_to_del = []
      # move triples to the right spot
      for k in wrong_entry_dict.keys():
          if k[0] in moved_groups:
              keys_to_del.append(k)
              continue
  
          if len(wrong_entry_dict[k]) == 3:
              new_Bg[k[1]] = Bg[k[0]]
              moved_groups.append(k[0])
              keys_to_del.append(k)
              if debug:
                  print(
                      f"MGTM: moved triple {wrong_entry_dict[k]} from group {k[0]} to group {k[1]}"
                  )
  
      for k in keys_to_del:
          del wrong_entry_dict[k]
      keys_to_del = []
  
      # move doubles
      for k in wrong_entry_dict.keys():
          # if we've already moved the group to which this key belongs, remove it
          if k[0] in moved_groups:
              keys_to_del.append(k)
              continue
  
          if len(wrong_entry_dict[k]) == 2:
              if (
                  len(new_Bg[k[1]]) == 0
              ):  # move it to its requested destination if possible
                  new_Bg[k[1]] = Bg[k[0]]
                  keys_to_del.append(k)
                  assert k[0] not in moved_groups
                  moved_groups.append(k[0])
                  if debug:
                      print(
                          f"MGTM: moved double {wrong_entry_dict[k]} from group {k[0]} to its preferred group {k[1]}"
                      )
              elif (
                  len(new_Bg[k[0]]) == 0
              ):  # otherwise leave it where it is (if possible)
                  new_Bg[k[0]] = Bg[k[0]]
                  keys_to_del.append(k)
                  assert k[0] not in moved_groups
                  moved_groups.append(k[0])
                  if debug:
                      print(
                          f"MGTM: left double {wrong_entry_dict[k]} where it was in group {k[0]}"
                      )
      for k in keys_to_del:
          del wrong_entry_dict[k]
      keys_to_del = []
  
      # move singles
      # try to leave things where they are to prevent oscillating
      for k in wrong_entry_dict.keys():
          if k[0] in moved_groups:
              keys_to_del.append(k)
              continue
  
          if len(new_Bg[k[1]]) == 0:  # requested destination
              new_Bg[k[1]] = Bg[k[0]]
              keys_to_del.append(k)
              assert k[0] not in moved_groups
              moved_groups.append(k[0])
              if debug:
                  print(
                      f"MGTM: moved single {wrong_entry_dict[k]} from group {k[0]} to its preferred group {k[1]}"
                  )
  
          elif len(new_Bg[k[0]]) == 0:
              new_Bg[k[0]] = Bg[k[0]]
              keys_to_del.append(k)
              assert k[0] not in moved_groups
              moved_groups.append(k[0])
              if debug:
                  print(
                      f"MGTM: left group {wrong_entry_dict[k]} where it was in group {k[0]}"
                  )
  
      for k in keys_to_del:
          del wrong_entry_dict[k]
      keys_to_del = []
  
      # put what's left where it'll fit
      for k in wrong_entry_dict.keys():
          if k[0] in moved_groups:
              keys_to_del.append(k)
              continue
  
          for dst in range(len(new_Bg)):
              if len(new_Bg[dst]) == 0:
                  new_Bg[dst] = Bg[k[0]]
                  keys_to_del.append(k)
                  assert k[0] not in moved_groups
                  moved_groups.append(k[0])
                  if debug:
                      print(
                          f"MGTM: put group {wrong_entry_dict[k]} where it found a spot in group {dst}"
                      )
                  break
  
      for k in keys_to_del:
          del wrong_entry_dict[k]
      keys_to_del = []
  
      assert len(wrong_entry_dict) == 0
      Agsize = sum([len(group) for group in Ag])
      Bgsize = sum([len(group) for group in new_Bg])
      assert Agsize == Bgsize
      new_B = [item for sublist in new_Bg for item in sublist]
      return new_B
  
imports: import numpy
  import time
  import subprocess
  import math
label_desc: Use image node_id 15 for calling a global function with example usage: move_groups_to_match(B, A, debug) and returns: new_B