title: summing vectors in Python
body: <p>Hi I am trying to add the 3rd column in the following example of input:</p>
  
  <p>input1:</p>
  
  <pre><code>act hi  1
  act bye 2
  act ciao    5
  </code></pre>
  
  <p>input2:</p>
  
  <pre><code>art hi  1
  art bye 2
  art kiss    5
  </code></pre>
  
  <p>with the following desired output:</p>
  
  <pre><code>act-art hi  2
  act-art bye 4
  act-art kiss    5
  act-art ciao    5
  </code></pre>
  
  <p>Below is the code that I have been working with.</p>
  
  <pre><code>def sumVectors(classB_infile, classA_infile, outfile):
  
      class_dictA = {}
  
      with open(classA_infile, "rb") as opened_infile_A:
          for line in opened_infile_A:
              items = line.split()
              classA, feat, valuesA = items[:3]
              class_dictA[feat] = float(valuesA)
  
  
      class_dictB = {}
  
      with open(classB_infile, "rb") as opened_infile_B:
          for line in opened_infile_B:
              items = line.split()
              classB, feat, valuesB = items[:3]
              class_dictB[feat] = float(valuesB)
  
  
  #print classA, classB, feat, sumVectors
  
  ####outfile 
      with open(outfile, "wb") as output_file:
          for key in class_dictA:
              if key in class_dictB:
                  weight = class_dictA[key] + class_dictB[key]
                  #outstring = "\t".join([classA + "-" +  classB, key, str(weight)])
              else:
                  weight = class_dictA[key]
                  outstring = "\t".join([classA + "-" +  classB, key, str(weight)])
                  output_file.write(outstring + "\n")
  
          for key in class_dictB:
              if key in class_dictA:
                  weight = class_dictB[key]
              outstring = "\t".join([classA + "-" + classB, key, str(weight)])
              output_file.write(outstring + "\n")
  </code></pre>
  
  <p>However, it gives me the following output:</p>
  
  <pre><code>act-art stress  5.0
  act-art bye 2.0
  act-art hi  1.0
  act-art kiss    1.0
  </code></pre>
  
  <p>Any insight as to why it is not summing the values in common in the 2nd column?
  Thank you</p>