简体   繁体   English

由一串元素 python 形成化学式

[英]Forming a chemical formula from a string of elements python

I want to ask a question about converting a list of items into a string.我想问一个关于将项目列表转换为字符串的问题。

I have the following list of a SiH2O molecule, where the 2 implies the subscript of 2 hydrogen atoms:我有以下 SiH2O 分子列表,其中 2 表示 2 个氢原子的下标:

[['Si', array([0, 1, 2])], 
['H', array([3, 4, 5])], 
['O', array([6, 7, 8])],
['H', array([3, 4, 5])]]

My intention is to convert this data into a chemical formulae ie SiH2O.我的目的是将这些数据转换为化学公式,即 SiH2O。

Another example is shown below:另一个例子如下所示:

['H', array([3, 4, 5])], 
['F', array([6, 7, 8])],
['H', array([3, 4, 5])]]

I am attempting to convert this to H2F (without any subscript formatting - I purely want to reach the H2F output.)我正在尝试将其转换为H2F (没有任何下标格式 - 我纯粹想达到H2F output。)

My attempt so far is as follows:到目前为止,我的尝试如下:

I first looped through the string to retrieve all the chemical symbols:我首先遍历字符串以检索所有化学符号:

symbols = []
for item in string:
    symbols.append(item[0])
symbols

I then tried to find the unique atoms in the string (ie a string of elements that are only repeated once in the string):然后我试图在字符串中找到唯一的原子(即在字符串中只重复一次的一串元素):

unique = []
for i in symbols:
    if i not in unique:
        unique.append(i)

And this returned这又回来了

['Si', 'H', 'O']

and

['H', 'F'] 

respectively.分别。

I attempted to create a dictionary of the elements and their count, with an original default of 0:我试图创建一个元素及其计数的字典,原始默认值为 0:

myDict = {key:0 for key in unique}

and then attempted to count through the dictionary.然后试图通过字典数数。

for item in symbols:
    count = myDict[item]
    count += 1
    myDict[item] = count

This returns:这将返回:

{'Si': 1, 'H': 2, 'O': 1}

Now, I want to use the key, value pair to compile the string SiH2O .现在,我想使用key, value对来编译字符串SiH2O I used the condition if value == 1 then I would not attach the subscript number after the chemical symbol.我使用了if value == 1的条件,那么我不会在化学符号后附加下标数字。

This was the code I attempted.这是我尝试的代码。

chemical_string = ""
for key, value in symbols:
    if value == 0:
        chemical_string += key
    else:
        chemical_string += key + "" + value

I expected the result SiH2O but I fall into this error:我期待结果SiH2O但我陷入了这个错误:

ValueError                                Traceback (most recent call last)
<ipython-input-57-d5312c683c2e> in <module>
      1 chemical_string = ""
----> 2 for key, value in myDict:
      3     if value == 0:
      4         chemical_string += key
      5     else:

ValueError: not enough values to unpack (expected 2, got 1)

I'm confused on why this doesn't work.我很困惑为什么这不起作用。 How can I solve this matter?我该如何解决这个问题?

As per @Luka Mensaric's answer, I need to use the .items() method.根据@Luka Mensaric 的回答,我需要使用.items()方法。

I also realised I need to convert value into a str inside the else statement:我还意识到我需要在else语句中将value转换为str

chemical_string = ""
for key, value in myDict.items():
    if value == 1:
        chemical_string += key
    else:
        chemical_string += key + "" + str(value)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM