I'm working with an xml file in python.here is the coed that i have:

import sys
from xml.dom.minidom import parse

# Load XML into tree structure                                                                                                          
tree = parse(sys.stdin)

#Find all VECTOR_AVERAGE nodes                                                                                                          
va_list = tree.getElementsByTagName('SIMULATION')[0].getElementsByTagName('AVERAGES')[0].getElementsByTagName('VECTOR_AVERAGE')

#Find the 'Density Correlations' node in the list of VECTOR_AVERAGEs                                                                    
for va in va_list:
    if va.attributes.getNamedItem('name').nodeValue == 'Density Correlations':
        density_correlations = va
#Get a list of all the SCALAR_AVERAGES                                                                                                  
sa_list = density_correlations.getElementsByTagName('SCALAR_AVERAGE')

#Initialize lists for holding the data                                                                                                  
indexvalue_list = []
mean_list = []
error_list = []
variance_list = []
autocorr_list = []
newerror_list = []

#Iterate over all SCALAR_AVERAGEs and put their values into the lists                                                                   
for sa in sa_list:
    x = sa.attributes.getNamedItem('indexvalue').nodeValue
    tuples = [eval(item) for item in x.split("--")]
    if tuples[0] == tuples[1]:
        newerror_list = error_list * error_list

print "#indexvalue MEAN\tERROR\tVARIANCE\tAUTOCORR\tNewError"
for i in range(len(mean_list)):
    print "%s\t%g\t%g\t%g\t%g\t%g" % (indexvalue_list[i],mean_list[i],error_list[i],variance_list[i],autocorr_list[i],newerror_list[i])

I'm extracting data form the file and need to do some math process on them. but they apparently are not int. Cause i'm getting the following error:

newerror_list = error_list * error_list
TypeError: can't multiply sequence by non-int

Any suggestion how i can solve this problem?

Edited by YasaminKh: n/a

8 Years
Discussion Span
Last Post by YasaminKh

What you seem to be trying to do is multiply a list by another list.
What do you expect the results to be?


I see. I want to multiply just one entery by itself.

I'm new to Python but in C++ there is this array that you can multiply like this:

array error[];

newerror = error* error ;

i need to the same thing here.


Wait, just one entry?
Then do the same as in C++.

You can do this: new_error_list[i] = error[i]**2 #squared

Edited by jcao219: n/a


That would be probably then most pythonically:

new_error_list = [err*err for err in error]

err**2 is same as err*err of course if you prefer.

And in Python it is common to reuse the list and not to save result with new name (error instead of new_error_list), but sometimes it is clearer to do like above. It could be actually clearer, if you do not continue to use old error list after this operation to update error.

Edited by pyTony: n/a


I'm using it, writing like this:

for i in range(len(mean_list)):
    newerror_list[i] = error_list[i] ** 2
    print "%s\t%g\t%g\t%g\t%g\t%g" % (indexvalue_list[i],mean_list[i],error_list[i],variance_list[i],autocorr_list[i],newerror_list[i])

but it gives me this error:
File "First.py", line 43, in ?
newerror_list = error_list ** 2
IndexError: list assignment index out of range

Any idea?

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.