【问题标题】:Python 2.7 run combinations on if, elif, elif statementsPython 2.7 在 if、elif、elif 语句上运行组合
【发布时间】:2017-08-17 16:34:44
【问题描述】:

我正在尝试创建一个显示碳原子连接性的代码(表示为 3D 坐标)

我的代码输入了一个 excel 文件,该文件组织为:

carbon x_coord y_coord z_coord 1 1.08 0.49 0.523 2 0.18 1.3 0.5 3 0.83 0.72 0.44

使用

subset = cmd[['carbon','x_coord', 'y_coord','z_coord']]
coordinate_values = [tuple(x) for x in subset.values]
atoms = coordinate_values
atomPairs = itertools.combinations(atoms, 2)

我计算了每个组合对之间的距离并排序如下:

if d >= 1.4 and d < 1.6:
   print("The bond is a single bond")
elif d >= 1.2 and d < 1.4:
   print("The bond is a double bond")
elif d >= 1 and d < 1.2:
   print("The bond is a triple bond")
else:
    print("No bond exists")

获取输出(对于每一对,下面只显示一个)

Computing distance between carbon_1 and carbon_1
The bond is a single bond

我现在想采用所有这些距离,但只允许每个碳_ 最多有四个键(四个“单”,或两个“单”和一个“双”,或一个“单”和一个“三” "),但不需要四个键。

虽然我不知道该怎么做,但我想得到类似这样的输出:

carbon_1 is bonded to
single bond to carbon_2
single bond to carbon_3
single bond to carbon_4
carbon_1 is bonded to
single bond to carbon_2
triple bond to carbon_5
carbon_2 is bonded to
single bond to carbon_1
triple bond to carbon_6

由于我的距离很长,我预计每个碳会有多种键组合_

我的想法是做另一个 if/elif 语句来实现这个输出,但我很不确定如何开始我的这个项目的代码。任何帮助是极大的赞赏!

【问题讨论】:

  • 如果债券正好是 1.4 会发生什么(是的,浮点数通常不是那么精确),但我想知道?此外,1.21.21 之间也存在差距...
  • 我使用的点不太可能,但你说得对,我会解决这个问题
  • 提示:你可以写d &gt;= 1.4 and d &lt; 1.6而不是1.4 &lt;= d &lt; 1.6
  • 我不确定我是否明白你在问什么。你能添加一些伪代码或一些东西来演示吗?
  • 您能向我们展示一些示例数据,以及您对这些数据的期望输出吗?

标签: python python-2.7 if-statement


【解决方案1】:

部分问题可以在 1.4 页边距内。

是的,在芳香循环中,距离已知为 1.4。如果仅由于舍入误差,距离计算为 1.399999999 怎么办?最终得到 6 个结合碳,图中编号为 4a 和 8a:

【讨论】:

    猜你喜欢
    • 2017-07-03
    • 1970-01-01
    • 1970-01-01
    • 2020-10-28
    • 2015-01-16
    • 1970-01-01
    • 2022-11-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多