title: Grouping data in Python
body: <p>I have the following (space delimited) input:</p>
  
  <pre><code>2012-10-05 PETER 6  
  2012-10-05 PETER 4  
  2012-10-06 PETER 60
  2012-10-06 TOM 10  
  2012-10-08 SOMNATH 80 
  </code></pre>
  
  <p>And I would like to achieve the following pipe-delimited output: 
  (where the columns are [DATE AND NAME, NUM ENTRIES, SUM OF LAST COL])</p>
  
  <pre><code>2012-10-05 PETER|2|10  
  2012-10-06 PETER|1|60  
  2012-10-06 TOM|1|10  
  2012-10-08 SOMNATH|1|80  
  </code></pre>
  
  <p>This is my code so far:</p>
  
  <pre><code>s = open("output.txt","r")
  fn=s.readlines()
  d = {}
  for line in fn:
   parts = line.split()
   if parts[0] in d:
     d[parts[0]][1] += int(parts[2])
     d[parts[0]][2] += 1
   else:
  d[parts[0]] = [parts[1], int(parts[2]), 1]
  for date in sorted(d):
     print "%s %s|%d|%d" % (date, d[date][0], d[date][2], d[date][1])
  </code></pre>
  
  <p>I am getting the output as:</p>
  
  <pre><code>2012-10-06 PETER|2|70
  </code></pre>
  
  <p>instead of  </p>
  
  <pre><code>2012-10-06 PETER|1|60
  </code></pre>
  
  <p>and <code>TOM</code> isn't showing in the list.</p>
  
  <p>What do I need to do to correct my code?</p>