【问题标题】:How can chemical formulas with '()' be broken down into their constituent elements?带 \'()\' 的化学式如何分解成它们的组成元素?
【发布时间】:2023-01-17 08:32:07
【问题描述】:

我想从化学式创建组成元素数据集。 我能够分解没有 () 的化学式,但我无法为带有 () 的化学式编写代码。

没有 () 的代码如下。

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

例如,我想将化学式“Ti3(SbPd)2”拆分为以下内容。

M1 M2 M3 M1_num M2_num M3_num
Ti Sb Pd 3 2 2

【问题讨论】:

  • 您需要手动检查大括号,因为它们被认为是“小写字母”。使用您当前的代码,大括号将触发 elif。在现有的elif之上添加另一个条件:if c in ['(', ')']: continue
  • @pavel:括号(和大括号)不被视为小写。他们会打else

标签: python pandas


【解决方案1】:

我把你的代码放在一个函数中:

def chemical(chemical_formula):
  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)
  return(comp_list,wt_list)

有了这个函数,我做了另一个函数,用括号标准化公式:

def standardization(chemical_formula1):
  comp_list = []
  wt_list = []
  chF = ''
  parentheses = False
  add_parentheses = False
  chemical_formula = ''
  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 ================
      comp_list = chemical(chemical_formula)[0]
      wt_list = chemical(chemical_formula)[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)
    #===========================================================================
  return(chF)

最后,一个函数可以根据需要在数据框中提供输出:

import pandas as pd

def output(chemical_formula):
  if '(' in list(chemical_formula):
    chemical_formula = standardization(chemical_formula)
  columns = []
  comp_list = chemical(chemical_formula)[0]
  wt_list = chemical(chemical_formula)[1]
  for i in range(1 , len(comp_list)+1):
    columns.append('M'+str(i))
  for i in range(1 , len(wt_list)+1):
    columns.append('M'+str(i)+'_num')
  df = pd.DataFrame(columns = columns)
  lst = list(comp_list + wt_list)
  df.loc[0] = lst
  return(df)
for formula in ['H20','Cu3(PO4)2','Ti3(SbPd)2','NaCl','Al(OH)3','C3H6O']:
  df = output(formula)
  print(df.to_string(index=False))
  print('-------------------------------')

输出:

M1 M2 M1_num M2_num
 H         2      0
-------------------------------
M1 M2 M3 M1_num M2_num M3_num
Cu  P  O      3      2      8
-------------------------------
M1 M2 M3 M1_num M2_num M3_num
Ti Sb Pd      3      2      2
-------------------------------
M1 M2 M1_num M2_num
Na Cl      1      1
-------------------------------
M1 M2 M3 M1_num M2_num M3_num
Al  O  H      1      3      3
-------------------------------
M1 M2 M3 M1_num M2_num M3_num
 C  H  O      3      6      1
-------------------------------

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-26
    • 2017-08-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多