-1

I have two text files that contain 1,096 values (these are features extracted from a neural network layer).

I want to take the first element of the the first text file and subtract it from the first element of the second text file and so on through all the 1,096 decimal values.

I then want to take the sum of these subtractions and store it in a variable for later use.

I am new to Python so I'm unsure which is the best way to access each element - I am aiming for some method similar to the euclidean distance method.

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
BLogger48
  • 21
  • 4
  • 1
    When you say "decimal" do you mean integers or are there decimal points? Examples help... how about showing sample files with 3 numbers each, and the sum you want. If you want people to test their answers, its polite to give them the data. – tdelaney Apr 14 '17 at 16:05

1 Answers1

1

Assuming your files are 1.txt and 2.txt

import Decimal as dc

with open('1.txt','rb') as fin1, open('2.txt','rb') as fin2:
  sub_sum = 0
  for x,y in zip(fin1,fin2):
    sub_sum += dc.Decimal(x) - dc.Decimal(y)
mechanical_meat
  • 163,903
  • 24
  • 228
  • 223