title: To form clusters based on the condition?
body: <p>The sample input file(the actual input file contains about 50,000 entries):</p>
  
  <pre><code>615 146 
  615 180 
  615 53  
  615 42  
  615 52  
  615 52  
  615 51  
  615 45  
  615 49
  616 34
  616 44
  616 42
  616 41
  616 42
  617 42
  617 43
  617 42
  685 33
  685 33
  685 33
  686 33
  686 33
  687 47
  687 68
  737 449
  737 41
  737 1138
  738 46
  738 53  
  </code></pre>
  
  <p>I must compare each value in column with same values like 615,615,615 must be grouped together the cluster must contain column1 value like 146,180.....45,49 then the cluster must break &amp; form the another cluster for next set of same values 616,616,616..........so on</p>
  
  <p>The code which i wrote was:</p>
  
  <pre><code>from __future__ import division
  from sys import exit
  h = 0
  historyjobs = []
  targetjobs = []
  
  
  def quickzh(zhlistsub,
      targetjobs=targetjobs,num=0,denom=0):
  
   li = [] ; ji = []
   j = 0
   for i in zhlistsub:
      x1 = targetjobs[j][0]
  
      x = targetjobs[i][0]
  
      num += x
      denom += 1
      if x1 &gt;= 0.9 * (num/denom):#to group all items with same value in column 0 
        li.append(targetjobs[i][1])
      else:
        break     
   return li
  
  
   def filewr(listli):
   global h
   s = open("newout1","a")
   if(len(listli) != 0):
        h += 1
        s.write("cluster: %d"%h)
        s.write("\n")
        s.write(str(listli))
        s.write("\n\n")
   else:
        print "0"
  
  
  def new(inputfile,
  historyjobs=historyjobs,targetjobs=targetjobs):
  zhlistsub = [];zhlist = []
  k = 0 
  
  with open(inputfile,'r') as f:
      for line in f:
          job = map(int,line.split())
          targetjobs.append(job)
      while True: 
       if len(targetjobs) != 0:
  
         zhlistsub = [i for i, element in enumerate(targetjobs)]
  
         if zhlistsub:
            listrun = quickzh(zhlistsub)
            filewr(listrun)
         historyjobs.append(targetjobs.pop(0))
         k += 1
       else:
           break
  
  new('newfinal1')
  </code></pre>
  
  <p>output which i got is:</p>
  
  <pre><code> cluster: 1
   [146, 180, 53, 42, 52, 52, 51, 45, 49, 34, 44, 42, 41, 42, 42, 43, 42, 33, 33, 33, 33, 33, 47, 68, 449, 41, 1138, 46, 53]
  
   cluster: 2
   [180, 53, 42, 52, 52, 51, 45, 49, 34, 44, 42, 41, 42, 42, 43, 42, 33, 33, 33, 33, 33, 47, 68, 449, 41, 1138, 46, 53]
  
   cluster: 3
   [53, 42, 52, 52, 51, 45, 49, 34, 44, 42, 41, 42, 42, 43, 42, 33, 33, 33, 33, 33, 47, 68, 449, 41, 1138, 46, 53]
   ..................so on
  </code></pre>
  
  <p>But the output that i require is:</p>
  
  <pre><code>  cluster: 1
    [146, 180, 53, 42, 52, 52, 51, 45, 49]
    cluster: 2
    [34, 44, 42, 41, 42]
    cluster: 3
    [42, 43, 42]
    _____________________ so on
  </code></pre>
  
  <p>So can anyone suggest what changes should i made to condition to obtain the required result .It would be really helpful ?</p>