简体   繁体   English

Python:4个循环,比较列表项

[英]Python: 4 for cycles, comparing list items

I have a quite scary algorithm to compare distances between atoms, however it works not the i want it to work. 我有一个非常可怕的算法来比较原子之间的距离,但它不起作用我想让它起作用。 Here is the code: 这是代码:

for k in ResListA:
  for n in ResListB:
    for m in ResListA[counter3].atoms:
        for z in ResListB[counter4].atoms:
            coordDist = distance.distance(ResListA[counter3].atoms[counter4],ResListB[counter2].atoms[counter1])
            counter1 = counter1 + 1
        counter1 = 0         
        counter4 = counter4 + 1 
    counter4 = 0
    counter2 = counter2 + 1
  counter2 = 0
  counter3 = counter3 + 1

Basically i want that minimal distance between 基本上我想要之间的最小距离

ResListA[0].atoms[0,..,n] ResListA [0] .atoms [0,..,n]的

ResListB[0,..,k].atoms[0,..,m] ResListB [0,..,K] .atoms [0,..,M]

to be calculated. 要计算。 However, it calculates 但是,它计算

ResListA[0].atoms[0] ResListA [0] .atoms [0]

to

ResListB[0,..,k].atoms[0,..,m] ResListB [0,..,K] .atoms [0,..,M]

For example: 例如:

ResListA[N,P,C,N,C] ResListB[C,C][P,P]... ResListA [N,P,C,N,C] ResListB [C,C] [P,P] ......

It should be 它应该是

dist(N,C) dist(N,C) dist(P,C) dist(P,C) dist(N,C)dist(N,C)dist(P,C)dist(P,C)

not

dist(N,C) dist(N,C) dist(N,P) dist (N,P) dist(N,C)dist(N,C)dist(N,P)dist(N,P)

Thank you in advance. 先感谢您。

I think your code can be written more like this. 我认为你的代码可以写得更像这样。

for k in ResListA:
    for n in ResListB:
        for m in k.atoms:
            for z in n.atoms:
                coordDist = distance.distance(m.atoms, z.atoms)

no idea what distance.distance does. 不知道distance.distance做了什么。 Shouldn't you be doing something with coordDist involving min() ? 你不应该用coordDist做一些涉及min()吗?

While gnibbler is probably correct in that is what you should do, this is what your current code simplifies to: 虽然gnibbler可能是正确的,但这是你应该做的,这是你当前的代码简化为:

for k in ResListA:
    for n in ResListB:
        for counter4, m in enumerate(k.atoms):
            for counter1, z in enumerate(ResListB[counter4].atoms):
                coordDist = distance.distance(m, n.atoms[counter1])

Your problem is that you need: 你的问题是你需要:

for z in ResListB[counter2].atoms:

instead of 代替

for z in ResListB[counter4].atoms:

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

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