I have some values in each line of a txt file. Now I want to calculate the difference between
line[1] - line[0], line[3] - line[2]
and so forth.
import sys
l = []
i = 0
f=open('Isotop1.txt')
# i = Zeilennummer, line = text der Zeile
for line in enumerate(f):
l.append(line)
for i in l:
c = l[i] - l[i-1]
print c
f.close()
later I want to store the solutions in a new text file.
but now I get the list indices must be integers, not tuple error.
Can someone help?
this is a small sample from the text file. I want to calculate the differences between 33 and 0, 94 and 61 and so on. Maybe I used a completey wrong approach to this problem...
0
33
61
94
122
153
178
200
227
246
274
297
310
324
Output:
If you want to use each number:
Or iterate over and get one number at a time:
Output:
Using
0as a default value tonextwill avoid a StopIterationError.If you are doing a lot of numerical computation numpy might be better: