首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >python中的If else if else条件循环

python中的If else if else条件循环
EN

Stack Overflow用户
提问于 2015-01-16 10:21:36
回答 1查看 115关注 0票数 0

我已经在molecules_group1中给出了一组分子,我想通过我的函数将这些分子移动到其他新的空molecules_group2上。它只在空的molecule_group2中添加了一个分子。每当我上传包含一个或多个分子的任何分子组时,它总是打印“排序的前五个分子存在于新组”,而不添加分子。此外,如果由于耐受性标准而没有添加分子,那么我如何使函数再次运行,直到成功添加分子。

代码语言:javascript
复制
def initial_configurations(newemptygroup,backgroundmols,tolerance,boxspace):
    p = PointRef(backgroundmols.molecule().evaluate().centerOfGeometry())
    c = CloseMols(p,newemptygroup,5)
    g_random = c.closeMolecules()
    if len(g_random) == 0:
        newemptygroup.add(backgroundmols)
        print("first molecules to be added")
    else:
        g = sorted(g_random.items(), key=operator.itemgetter(1)) # sort the dict by value which give tuple
        print("sorted first five  molecules present in newgroup =%s" %g)
        t = [x[0] for x in g]
        selected = newemptygroup[t[0]].molecule().evaluate().centerOfGeometry()
        for i in range(1,len(t)):
            closemols = newemptygroup[t[i]].molecule().evaluate().centerOfGeometry()
            if (selected-closemols).length() > tolerance:
                newemptygroup.add(backgroundmols)
            else:
                print("not added")           
    return newemptygroup
EN

回答 1

Stack Overflow用户

发布于 2015-01-16 10:37:57

你可以在newemptygroup.add(backgroundmols)后面插入一条break语句

代码语言:javascript
复制
...
if (selected-closemols).length() > tolerance:
  newemptygroup.add(backgroundmols)
  break
else:
  print("not added")   
...

这假设您会在for循环中的某个时候找到正确的背景工具。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27976353

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档