Extract value and average

MRAB python at mrabarnett.plus.com
Mon Jun 8 12:50:15 EDT 2009


Steven D'Aprano wrote:
> On Mon, 08 Jun 2009 18:13:50 +0200, Francesco Pietra wrote:
> 
>> I come 'naked', which is unusual and unfair.
> 
> ???
> 
>> However, I find it
>> difficult to give a correct start. The files consist, among other
>> things, of a huge number of blocks of the type
>>
>>
>>  NSTEP =     1000   TIME(PS) =     152.000  TEMP(K) =   298.54  PRESS = 
>>    89.4 Etot   =   -134965.2123  EKtot   =     41282.1781  EPtot      = 
>>   -176247.3905 BOND   =      1771.7644  ANGLE   =      6893.3003  DIHED 
>>      =      4660.1650 1-4 NB =      1931.6071  1-4 EEL =      7799.8343 
>>  VDWAALS    =     19047.1551 EELEC  =   -218354.9960  EHBOND  =        
>>  0.0000  RESTRAINT  =         3.7793 EAMBER (non-restraint)  =  
>>  -176251.1698 EKCMT  =     16048.2253  VIRIAL  =     14755.8154  VOLUME 
>>     =    669299.5681
>>                                                     Density    =        
>>                                                     0.9896
>>  Ewald error estimate:   0.8252E-05
>>
>>
>>
>> (in attachment what surely is a correct reproduction of columns)
>>
>> I would like to extract values corresponding to variable DIHED (here
>> 4660.1650) and getting also the mean value from all DIHED.
>>
>> Thanks for giving a possible attack
> 
> 
> Assuming no DIHED value will ever be split over two lines:
> 
> 
> data = open(filename)
> values = []
> for line in data:
>     if line and line.strip():  # ignore blanks
>          words = line.strip().split()
>          try:
>              i = words.index("DIHED")
>          except IndexError:
>              continue
>          values.append(float(words[i+2]))
> mean = sum(values)/len(values)
> 
> 
> should do the job.
> 
str.index raises ValueError, not IndexError. Anyway, your code could be 
shortened slightly:

data = open(filename)
values = []
for line in data:
     words = line.split()
     try:
         i = words.index("DIHED")
         values.append(float(words[i + 2]))
     except ValueError:
         pass
mean = sum(values) / len(values)



More information about the Python-list mailing list