Im not even sure what exactly my problem is from this error. Any information would be very helpful.
what i have so far:
def equations(specie,elements):
vectors=[]
for x in specie:
vector=extracting_columns(x,elements)
vectors.append(vector)
When i run:
equations(['OH', 'CO2','c3o3','H2O3','CO','C3H1'],
['H', 'C', 'O'])
i get the following error:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File “_sage_input_77.py”, line 10, in
exec compile(u’print support.syseval(python, u”equations([\’OH\’, \’CO2\’,\’c3o3\’,\’H2O3\’,\’CO\’,\’C3H1\’], unel)”, SAGE_TMP_DIR)
File “”, line 1, in
File “/sagenb/sage_install/sage-5.4-sage.math.washington.edu-x86_64-Linux/devel/sagenb-git/sagenb/misc/support.py”, line 479, in syseval
return system.eval(cmd, sage_globals, locals = sage_globals)
File “/sagenb/sage_install/sage-5.4-sage.math.washington.edu-x86_64-Linux/local/lib/python2.7/site-packages/sage/misc/python.py”, line 56, in eval
eval(z, globals)
File “”, line 1, in
File “”, line 4, in equations
File “”, line 3, in extracting_columns
ValueError: need more than 1 value to unpack
my previous functions if needed:
import re
def parse_formula(formula):
”’Given a simple chemical formula, return a list of (element, multiplicity) tuples.
Example:
'H2SO4' --> [('H', 2.0), ('S', 1.0), ('O', 4.0)]
'''
return [ (elem, float(mul) if mul else 1.) for (elem, mul) in re.findall(r'([A-Z][a-z]*)(\d*)', formula) ]
def unique_element(group):
c=[]
for element in group:
piece=parse_formula(element)
for x in piece:
c.append(x[0])
return list(set(c))
def extracting_columns(specie, elements):
species_vector=zeros(len(elements))
for (el,mul) in specie:
species_vector[elements.index(el)]=mul
return species_vector
The problem is that you’re calling
extracting_columnswith a string like'OH'as the first argument, so when you try to dofor (el,mul) in specie:it’s trying to unpack the'O'into(el, mul).An easy way to debug this is to insert a
printright before the offending line:So, how is
extracting_columnsgetting'OH'? Well, let’s look at where it’s called, with a couple moreprints:Now, when you run it, you’ll see that
specieis['OH', 'CO2', 'c3o3', 'H2O3', 'CO', 'C3H1'], so the first element of it is obviously'OH'.As for how to fix this… well, without knowing what you’re actually trying to do, it’s hard to tell you how to do it. But obviously if you want to iterate over the first argument to
extracting_columnsand treat each item as a pair, you have to pass it a sequence of pairs rather than a string.But it looks like your
parse_formulais made specifically for turning a string like'OH'into a list of pairs like[('O', 1.0), ('H', 1.0)]. So presumably the problem is that you just forgot to call it somewhere. Maybe you wantequationsto look like this?This does something, without any exceptions. Whether it’s what you actually want, I have no idea.
At any rate, learning how to see what’s actually going on in your code and debug trivial problems is probably more important than getting the right answer here immediately.