简体   繁体   English

如果否则,则条件循环在Python

[英]If else if else conditional loop in python

I've set of molecules given in molecules_group1 and i want to move the molecules to other new empty molecules_group2 via my function. 我已经设置了Molecular_group1中给定的一组分子,并且我想通过我的函数将这些分子移至其他新的空Molecular_group2中。 It adds only one molecules to the empty molecule_group2. 它仅将一个分子添加到空的Molecular_group2中。 whenever i upload any moleculegroup containing one or more molecules it always prints "sorted first five molecules present in newgroup" without adding the molecule. 每当我上传包含一个或多个分子的任何分子组时,它总是打印“新组中存在的前五个分子”,而不添加该分子。 furthermore if the molecule is not added due to tolerance criteria then how i can make the function run again until it is successful to add the molecule. 此外,如果由于公差标准而未添加分子,那么我如何使功能再次运行,直到成功添加分子。

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

You could insert a break statement right after newemptygroup.add(backgroundmols) 您可以在newemptygroup.add(backgroundmols)之后插入break语句

...
if (selected-closemols).length() > tolerance:
  newemptygroup.add(backgroundmols)
  break
else:
  print("not added")   
...

This assumes that you will find the correct backgroundmols at sometime within your for loop. 假设您有时会在for循环中找到正确的backgroundmol。

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

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