2

I've got two files, one with just ID-numbers and the other with some of those ID-numbers along with their corresponding values (statistical info). I have a script that checks if the numbers are in both files then prints the numbers and values from the larger file. Unfortunately with my present script, I just get errors when it notices that things are 'out of range'. Furthermore, later in the script it tries to divide by zero I assume due to the values that aren't there (not in value-file). Here's the pertinent aspects of my script as is:

for j in range(len(data)/22):

  if(data[1+j*22][3]==stars[i][0]):
   name=str(data[1+j*22][1])
   dust=str(data[2+j*22][20])

I've tried this to no success:

for j in range(len(data)/22):
  if(data[1+j*22][3]!=stars[i][0]):
   print 'not available'
  elif(data[1+j*22][3]==stars[i][0]):
   name=str(data[1+j*22][1])
   dust=str(data[2+j*22][20])

Also this:

for j in range(len(data)/22):
  if(data[1+j*22][3]!=stars[i][0]):
   break
  elif(data[1+j*22][3]==stars[i][0]):
   name=str(data[1+j*22][1])
   dust=str(data[2+j*22][20])

And finally at the bottom, where it tries to divide by zero

print Temp
Teff=round(sum(Temp/chi)/sum(1/chi),0)
Tempin=round(sum(Tin/chi)/sum(1/chi),0)
tau=round(sum(Tau/chi)/sum(1/chi),5)
luminosity=round(sum(Luminosity/chi)/sum(1/chi),0) 
Massloss=round(sum(Mdot/chi)/sum(1/chi),3)
Chi=round(sum(chi)/len(chi),2)

where my 'chi' value is giving the problem (defined in script).

Here's the error I'm getting

if(data[1+j*22][3]!=stars[i][0]):
IndexError: list index out of range

I believe that if I can fix the initial data ranges I won't have to worry about the chi values being zero. I only mentioned it hoping that would give more clarification to my problem.

Trimmed code:

import numpy as np
import sys
import StringIO

#Read in the stars that will be selected from the set of models
#SCRIPT='/SAH/SAH5/user/Results/b3results/b3Over'
SCRIPT=raw_input('Star List file: Ex. /SAH/SAH5/user/Modeling/TestStars\n') 
stars=[]
f=open(SCRIPT,"rb") #Fill in the SCRIPT variable
for line in f:
 s=line.split()
 stars.append(s)
f.close()

#Read in the Models
#SCRIPTM='/SAH/SAH5/user/Results/b3/Filtered/NewModels'
SCRIPTM=raw_input('Uttenthaler output file: Ex. /SAH/SAH5/user/Modeling/chisquaretests/stars3\n')
data=[]
d=open(SCRIPTM,"rb") #Fill in the SCRIPT variable
for line in d:
 if not line.strip():
  continue
 else:
  s=line.split()
  data.append(s)

d.close()

NewResults=''


for i in range(len(stars)):
 print stars[i][0]
 chi=np.array([])
 Temp=np.array([])
 Tin=np.array([])
 Tau=np.array([])
 Luminosity=np.array([])
 Mdot=np.array([])
 #print len(data)
 for j in range(len(data)/22):

  if(data[1+j*22][3]==stars[i][0]):
   name=str(data[1+j*22][1])
   dust=str(data[2+j*22][20])

   #print data[2+j*22][4]
   chi=np.append(chi,float(data[2+j*22][4]))
   chi=np.append(chi,float(data[3+j*22][4]))
   chi=np.append(chi,float(data[4+j*22][4]))
   chi=np.append(chi,float(data[5+j*22][4]))
   chi=np.append(chi,float(data[6+j*22][4]))


 print Temp
 Teff=round(sum(Temp/chi)/sum(1/chi),0)
 Tempin=round(sum(Tin/chi)/sum(1/chi),0)
 tau=round(sum(Tau/chi)/sum(1/chi),5)
 luminosity=round(sum(Luminosity/chi)/sum(1/chi),0)
 Massloss=round(sum(Mdot/chi)/sum(1/chi),3)
 Chi=round(sum(chi)/len(chi),2)

 NewResults=NewResults+str(int(stars[i][0]))+'\t'+name+'\t'+str(Teff)+'\t'+str(Tempin)+'\t'+dust+'\t'+str(tau)+'\t'+str(luminosity)+'\t'+str(Massloss)+'\t'+str(Chi)+'\n'

print NewResults

This is my stars3 file, That is the 'chunk' of data for one of the integer values in the other file. Here it's in the 'Name' row: '6973'. TestStars is simply a single column of numbers: 6239, 7010, etc. No commas, just pure integers.

4

1 回答 1

0

我在这里注意到的一些事情。

如果你这样做了,data[1+j*22][3]那么你永远不会访问列表的第零个成员,你不想访问它吗?为什么你甚至在范围减速度中除以 22?

至于零除,那么你在 chi 上使用了 len 函数,但是在之前的几行中你使用 chi 作为除数,那么 chi 是数字还是列表?

于 2013-07-02T20:03:55.340 回答