[英]How can chemical formulas with '()' be broken down into their constituent elements?
I wanto to create constituent elements dataset from chemical formulas.我想从化学式创建组成元素数据集。 I was able to break down the chemical formulas without the (), but I am unable to write the code for the ones with the ().我能够分解没有 () 的化学式,但我无法为带有 () 的化学式编写代码。
The code without () is below.没有 () 的代码如下。
symbol = ''
comp_list = []
wt_list = []
for c in chemical_formula:
if c.isupper():
if len(symbol) != 0:
comp_list.append(symbol)
wt_list.append(1)
symbol = ''
symbol += c
elif c.islower():
symbol += c
else:
comp_list.append(symbol)
wt_list.append(int(c))
symbol = ''
if len(symbol) != 0:
comp_list.append(symbol)
wt_list.append(1)
comp_data[atom_cols] = comp_list
comp_data[comp_cols] = wt_list
and for example, I want to split chemical formula 'Ti3(SbPd)2' into below.例如,我想将化学式“Ti3(SbPd)2”拆分为以下内容。
M1 M1 | M2 M2 | M3 M3 | M1_num M1_num | M2_num M2_num | M3_num M3_num |
---|---|---|---|---|---|
Ti钛 | Sb锑 | Pd钯 | 3 3个 | 2 2个 | 2 2个 |
I didn't want to change your code, so in that case we have to return the string with "parenthesis" without it.我不想更改您的代码,所以在那种情况下,我们必须返回带有“括号”但不带它的字符串。 For this, I had to put your code in an "if" of my "for".为此,我不得不将您的代码放在我的“for”的“if”中。 However, I don't think we have reached a very optimized solution: (:但是,我不认为我们已经达到了一个非常优化的解决方案:(:
chemical_formula1 = 'Cu3(PO4)2'
chF = ''
parentheses = False
add_parentheses = False
chemical_formula = ''
symbol = ''
comp_list = []
wt_list = []
for i in chemical_formula1:
#====================================================================================================
if i == ')' :
parentheses = False
add_parentheses = True
elif i == '(' :
parentheses = True
elif parentheses == True:
chemical_formula += str(i)
#====================================================================================================
#==================== Adding the elements inside the '()' while "i" is the number after the ')' ====================
elif add_parentheses == True:
#==================== YOUR CODE TO BREAK CHEMICAL FORMULA ====================
for c in chemical_formula:
if c.isupper():
if len(symbol) != 0:
comp_list.append(symbol)
wt_list.append(1)
symbol = ''
symbol += c
elif c.islower():
symbol += c
else:
comp_list.append(symbol)
wt_list.append(int(c))
symbol = ''
if len(symbol) != 0:
comp_list.append(symbol)
wt_list.append(1)
#===========================================================================
for e in range(len(comp_list)):
chF += str(comp_list[e])
chF += str((int(wt_list[e]))*int(i))
add_parentheses = False
#==================================================================================================================================
#==================== If we don't have any parentheses, only this "if" will be run every time ====================
elif parentheses == False and add_parentheses == False:
chF += str(i)
#======================================================================================================================
print(chF)
Output: Output:
Cu3P2O8
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.