我可以使用一种更优雅/更优化的方法来制作此连接算法吗?

詹卢卡·辛坦尼(Gianluca Cientanni)

我制定了一种算法,可以从原子列表(给出它们彼此之间的距离)中找出分子中连接的原子。在物理环境之外考虑这个问题,它只是一个封闭的网络问题,其中节点是原子,而边是连接原子的原子键。我有一个节点列表,以及一个连接这些节点的边的列表,我需要找出每个唯一分子的列表。我已经在下面的代码中做到了这一点,但是它有点慢而且很丑陋。有没有一种方法可以优化此算法?

这是我的代码,可以使用相关信息供您尝试(我将提供另一个原子列表尝试进行操作,称为pairs_1和selected_atom_1,只需将pairs_1更改为pairs,将selected_atom_1更改为selected_atom即可正常工作)

pairs = [[0, 1],
 [0, 2],
 [3, 4],
 [3, 5],
 [6, 7],
 [6, 8],
 [9, 10],
 [9, 11],
 [12, 13],
 [12, 14],
 [15, 16],
 [15, 17],
 [18, 19],
 [18, 20],
 [21, 22],
 [21, 23],
 [24, 25],
 [24, 26],
 [27, 28],
 [27, 29],
 [30, 31],
 [30, 32],
 [33, 34],
 [33, 35],
 [36, 37],
 [36, 38],
 [39, 40],
 [39, 41],
 [42, 43],
 [42, 44],
 [45, 46],
 [45, 47]]

chosen_atom = [np.random.rand() for i in range(48)]

pairs_1 = [[0, 6],
 [1, 7],
 [2, 8],
 [3, 9],
 [4, 10],
 [5, 6],
 [5, 10],
 [6, 7],
 [7, 8],
 [8, 9],
 [9, 10]]

chosen_atom_1 = [np.random.rand() for i in range(11)]

# use list of lists to define unique molecules
molecule_list = []

for i in pairs:
    temp_array = []
    for ii in pairs:
        temp_pair = [i[0], i[1]]

        if temp_pair[0] == ii[0]:
            temp_array.append(ii[1])
            temp_array = set(temp_array)
            temp_array = list(temp_array)
        if temp_pair[1] == ii[1]:
            temp_array.append(ii[0])
            temp_array = set(temp_array)
            temp_array = list(temp_array)

        for iii in temp_array:
            for j in pairs:
                if iii == j[0]:
                    temp_array.append(j[1])
                    temp_array = set(temp_array)
                    temp_array = list(temp_array)
                if iii == j[1]:
                    temp_array.append(j[0])
                    temp_array = set(temp_array)
                    temp_array = list(temp_array)

        if len(temp_array) > len(chosen_atom):
            break
    molecule_list.append(temp_array)

molecule_list = [list(item) for item in set(tuple(row) for row in molecule_list)]

# the output of pairs should be
molecule_list = [[8, 6, 7],
 [27, 28, 29],
 [9, 10, 11],
 [0, 1, 2],
 [32, 30, 31],
 [18, 19, 20],
 [45, 46, 47],
 [33, 34, 35],
 [24, 25, 26],
 [42, 43, 44],
 [16, 17, 15],
 [12, 13, 14],
 [21, 22, 23],
 [3, 4, 5],
 [40, 41, 39],
 [36, 37, 38]]
# the output of pairs_1 should be:
molecule_list = [[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]]

因此,在上面我已经给出了我现在应该获得的输出-使该代码更好的任何想法将不胜感激。

迈科拉(Mykola Zotko)

正如我在评论中所说的那样,您需要连接的组件算法,并且可以使用该networkx轻松解决它

import networkx as nx

G = nx.from_edgelist(pairs)

print([i for i in nx.connected_components(G)])

# jupyter notebook
%matplotlib inline
nx.draw(G, with_labels=True)

输出:

[{0, 1, 2}, {3, 4, 5}, {8, 6, 7}, {9, 10, 11}, {12, 13, 14}, {16, 17, 15}, {18, 19, 20}, {21, 22, 23}, {24, 25, 26}, {27, 28, 29}, {32, 30, 31}, {33, 34, 35}, {36, 37, 38}, {40, 41, 39}, {42, 43, 44}, {45, 46, 47}]

图形

这是我的脚本,用于根据原子坐标构建和可视化分子图。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

一种更优雅的方法来实现基本的Groovy

有没有一种方法可以更优雅地编写此“ if”列表?

寻找一种更优雅的方法来通过表达式将MethodInfo转换为Func

寻找一种更优雅的方式来解决此任务

此python装饰器的一种更优雅的方式

我可以用一种更优雅的方式对一个组件执行多项操作吗?

一种使用绑定到多个组元素的数据来绑定新元素的更优雅的方法

寻找一种更优雅的方法来获取圆柱坐标中的二维numpy方位角数组

我可以使类型重定义更优化吗?

我可以使用Linq以比这更优雅的方式从集合中获取属性吗?

有什么更优雅的方法来解析此字符串?

我可以使用一种方法来返回SSRS报告代码中的字符串列表作为tablix中的标题吗?

是否有一种更优雅的解决方案来填充地图中的列表?

一种简单/更优雅的方式来在变量为nil时停止代码?

有没有一种更优雅的方式来代替编写大量查询?

有没有更好,更优化的方法来执行此SQL查询?

在几列SUM上应用嵌套排名的一种更优雅的方法

bash脚本:一种检索最新版本go的更优雅的方法?

是否有一种更优雅的方式来读取然后使用node js流写入*同一文件*

一种类型被强制转换为另一种类型,可以使用一种方法来确定接收器的类型吗?

我可以使用另一种方式来获取C ++时间吗?

在使用Guava转换Collection时,是否有一种优雅的方法来删除null?

if的序列可以更优雅地编写吗?

检查日期/期间是否在另一个日期/期间内-可以使用更优雅的解决方案吗?

是否有一种更优雅的方式来使用“ while循环”而不必先在“ while循环”之外定义变量?

寻求一种简单的方法来订购我的 dfs 来连接它们

C#更优雅的方法来检查异常是否在指定异常列表中?

有没有更优雅的方法来折叠 dplyr 中的两行?

更优雅的方法来避免对集合进行分组的这种单行简化的实现?