title: How to Collapse a List of Labels | Python
body: <p>So I realize this is both a theoretical question and a coding question, but say if I have a list of 10 labels (x1, x2,...,x10) and their corresponding "location" vectors (v1, v2, ..., v10). </p>
  
  <p>I want to collapse them based on their L2-norm distance from each other. For example, if v1 is close to v10, then relabel all x10's as x1's and so on. </p>
  
  <p>So the end result could hypothetically look like the new labels: (x1, x3, x7, x8). Is there a way to smartly just make this into (x1', x2', x3', x4')?, so that people don't get confused and assume the new labels are the same.</p>
  
  <p>Given: 
  labels = vector of Nx1 that has all the labels (1,2,3...,10)</p>
  
  <p>Example Code:</p>
  
  <pre><code>epsilon = 0.2  # defines distance
  change = [] # initialize vector of labels to change
  
  # matrix is NxN matrix of the pairwise distances between all our vectors (v1,..,v10)
  for i in range(0, distancematrix): 
      for j in range(0, distancematrix):
          # add all pairs of labels that are "close", so that we may relabel
          if i!=j and distancematrix[i, j] &lt; epsilon:
              change.append((i,j))
  </code></pre>
  
  <p>This will produce a list of pairs that I want to relabel. Is there a smart way of rewriting 'labels', so that it merges all the pairs I want to merge AND keeps the labels that were not part of any merge. Then reorganizes it to go from (1,2,3,4), if I merge 6 pairs of numbers (10-6 = 4).</p>
  
  <p>Thank you. I realize this is somewhat of a weird problem, so if you have questions please let me know!</p>