Python appending from previous for loop iteration -


i have simple annoying problem. reading in list of files 1 one names stored in ascii file ("file_input.txt") , performing calculations on them. issue when print out result of calculation ("print peak_wv, peak_flux" in script below) appends previous printout. below code have written, please me see i'm doing wrong here.

from math import*  wv = [] flux = [] fits = []  p = open("file_input.txt","r") line in p:     fits.append(str(line.split()[0])) p.close()  j in range(len(fits)):     f = open("%s"%(fits[j]),"r")     line in f:         wv.append(float(line.split()[0]))         flux.append(float(line.split()[1]))     f.close()     print "%s"%(fits[j])     in range(len(wv)):         if 6555.0<wv[i]<6569.0:             m1 = (flux[i+1]-flux[i])/(wv[i+1] - wv[i])             m2 = (flux[i+2]-flux[i+1])/(wv[i+2] - wv[i+1])             if m2*m1 < 0:                 peak_wv = (wv[i+2]+wv[i+1]+wv[i])/3.0                 peak_flux = flux[i+1]                 print peak_wv, peak_flux 

based on comment believe issue appending data each new file. want clear wv , flux each new file. example:

for j in range(len(fits)):     wv = []     flux = []     f = open("%s"%(fits[j]),"r") 

edit: should point out aren't using math functions don't need import, , there bunch of ways make code more pythonic. can use "with open" idiom avoid having manually close file. can use basic loops and/or "enumerate" make loops cleaner. example, this:

for j in range(len(fits)):     f = open("%s"%(fits[j]),"r")     # code     f.close() 

could this:

for file in fits:     open(file, "r") f:         # code 

and this:

for in range(len(wv)):     if 6555.0<wv[i]<6569.0: 

could be:

for i, cur_wv in enumerate(wv):     if 6555.0 < cur_wv < 6569.0: 

Comments

Popular posts from this blog

angular - Is it possible to get native element for formControl? -

unity3d - Rotate an object to face an opposite direction -

javascript - Why jQuery Select box change event is now working? -