这是我目前所拥有的:
def unique_element(group):
list=[]
for element in group:
piece=parse_formula(element)
for x in piece:
list.append(x[0])
return list #list(set(list))
我有下面的其他功能,但这是我要修复的功能。现在它返回一个带有字母列表的函数,但我不想重复字母。示例:
unique_element(['H2O2Y2','R3O2','Y2S3'])
['H', 'O', 'Y', 'R', 'O', 'Y', 'S']
我认为使用 list(set(list)) 会起作用,但是当我运行该函数时,我得到:
unique_element(['H2O2Y2','R3O2','Y2S3'])
追溯(最近的调用最后):
File "<stdin>", line 1, in <module>
File "_sage_input_61.py", line 10, in <module>
exec compile(u'print _support_.syseval(python, u"unique_element([\'H2O2Y2\',\'R3O2\',\'Y2S3\'])", __SAGE_TMP_DIR__)
File "", line 1, in <module>
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 <module>
File "", line 10, in unique_element
TypeError: 'list' object is not callable
其他功能:
from numpy import *
from scipy import *
from pylab import *
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) ]
请您参考如下方法:
您正在使用标准库函数作为变量名。这就是 set() 操作失败的原因。
将 list = [] 更改为 my_list = [] 或其他...
