Hi,

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
        break
#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                                                                   
#above.                                                                                                                                 
for sa in sa_list:
    x = sa.attributes.getNamedItem('indexvalue').nodeValue
    tuples = [eval(item) for item in x.split("--")]
    if tuples[0] == tuples[1]:
        indexvalue_list.append(tuples[0])
        #indexvalue_list.append(sa.attributes.getNamedItem('indexvalue').nodeValue)                                                     
	mean_list.append(float(sa.getElementsByTagName('MEAN')[0].childNodes[0].data))
        error_list.append(float(sa.getElementsByTagName('ERROR')[0].childNodes[0].data))
        variance_list.append(float(sa.getElementsByTagName('VARIANCE')[0].childNodes[0].data))
	autocorr_list.append(float(sa.getElementsByTagName('AUTOCORR')[0].childNodes[0].data))
        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?

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

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.

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?

Change line number 2 to

newerror_list.append(error_list[i]**2)

got it thanks. I had forgotten to initialize it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.