How can I loop through a list changing the list elements at each time step, adding or subtracting input values that are in the txt files?
Why don´t I get the following result, in this order? [-2.0, -1.0, 0.0, 1.0, 2.0] [-1.0, 0.0, 1.0, 2.0, 3.0] [-2.0, -1.0, 0.0, 1.0, 2.0], instead i get the second list in the wrong place. Can I edit list elements in a more consistent way, considering this form of input data? My objective is to change a initial list (V) at each time step, adding or subtracting the input values that are in the txt files.
V = [1,2,3,4,5]
f = open('Qin.txt') # values in Qin.txt: 1, 3, 2
g = open('Qout.txt') # values in Qout.txt: 4, 5, 5
for line in f:
Z=float(line)
for line in开发者_开发知识库 g:
G=float(line)
c = []
for i in range(len(V)):
c.append(V[i]+Z-G)
print c
OR:
V = [1,2,3,4,5]
f = open('Qin.txt') # values in Qin.txt: 1, 3, 2
fdata = f.readlines()
f.close()
g = open('Qout.txt') # values in Qout.txt: 4, 5, 5
gdata = g.readlines()
g.close()
output = [[v + float(x) - float(y) for v in V] for y in gdata for x in fdata]
print output
>>> [[-2.0, -1.0, 0.0, 1.0, 2.0], [-1.0, 0.0, 1.0, 2.0, 3.0], [-2.0, -1.0, 0.0, 1.0, 2.0]]
Or if in another case(in case of i misunderstand your formatting):
V = [1,2,3,4,5]
f = open('Qin.txt') # values in Qin.txt: 1, 3, 2
fdata = map(float, f.readlines())
f.close()
g = open('Qout.txt') # values in Qout.txt: 4, 5, 5
gdata = map(float, g.readlines())
g.close()
output = [[v+fdata[i]-y for v in V] for i,y in enumerate(gdata)]
OR if you want to modify V each step(note that it will not equal to result mentioned in your question so my code is just sample of how to do this):
V = [1,2,3,4,5]
f = open('Qin.txt') # values in Qin.txt: 1, 3, 2
fdata = map(float, f.readlines())
f.close()
g = open('Qout.txt') # values in Qout.txt: 4, 5, 5
gdata = map(float, g.readlines())
g.close()
for i,y in enumerate(gdata):
for j,v in enumerate(V):
V[j] = v + fdata[i] - y
print V
It's hard to tell exactly what is wrong with your current algorithm because the indentation appears to be lost. But I would suspect that it had something to do with reading every line in g once per every line in f, when it seems you want to use line 0 in f with line 0 in g and line 1 in f with line 1 in g, etc.
I think this algorithm will do what you are looking for...
V = [1,2,3,4,5]
f = open('Qin.txt') # values in Qin.txt: 1, 3, 2
g = open('Qout.txt') # values in Qout.txt: 4, 5, 5
fItems = []
for line in f:
fItems.append(float(line))
lineNum = 0
for line in g:
c = []
for i in range(len(V)):
c.append(V[i]+fItems[lineNum]-float(line))
print c
lineNum+=1
First of all, the fourth time you access f
you won't be getting 1
again because you have already read all of the data once. Therefore, you should read the data into the variables f
and g
before processing it. If you do the following:
f = open('Qin.txt').readlines()
g = open('Qout.txt').readlines()
Then you will get:
[-2.0, -1.0, 0.0, 1.0, 2.0]
[-3.0, -2.0, -1.0, 0.0, 1.0]
[-3.0, -2.0, -1.0, 0.0, 1.0]
[0.0, 1.0, 2.0, 3.0, 4.0]
[-1.0, 0.0, 1.0, 2.0, 3.0]
[-1.0, 0.0, 1.0, 2.0, 3.0]
[-1.0, 0.0, 1.0, 2.0, 3.0]
[-2.0, -1.0, 0.0, 1.0, 2.0]
[-2.0, -1.0, 0.0, 1.0, 2.0]
These are the results you'll get from the calculations you specified above.
精彩评论