0

假设我有一个使用 defaultdict 创建的字典,像这样(几百点长):

L [(32.992, 22.861, 29.486, 'TYR'), (32.613, 26.653, 29.569, 'VAL'), (30.029, 28.873,     27.872, 'LEU')
A [(1.719, -25.217, 8.694, 'PRO'), (2.934, -21.997, 7.084, 'SER'), (5.35, -19.779, 8.986, 'VAL')
H [(-0.511, 19.577, 27.422, 'GLU'), (2.336, 18.416, 29.649, 'VAL'), (2.65, 19.35, 33.322, 'GLN')

然后我想遍历每个键中的每个值,并检查从该值到其他键下每个其他残基的距离。我知道如何使用一个简单的公式来检查距离,但是我在让循环正常工作时遇到了问题。理想情况下,它将检查 L 中的每个点与 A 和 H 中的每个点,然后继续检查 A 中的 each_value 与 L 和 H 等...

我尝试过简单的理解,但在完成之前我总是只得到一条链来检查另一条链。任何帮助表示赞赏。

我正在从这样的 PDB 文件中提取坐标:

xyz = []

for line in pdb_file:
    if line.startswith("ATOM"):
        # get x, y, z coordinates for Cas
        chainid = str((line[20:23].strip()))
        atomid = str((line[16:20].strip()))
        pdbresn= int(line[23:26].strip())
        x = float(line[30:38].strip())
        y = float(line[38:46].strip())
        z = float(line[46:54].strip())
        if line[12:16].strip() == "CA":
            xyz.append((chainid,x,y,z,atomid))

然后将其放入这样的字典中:

d = defaultdict(list)
for c,x,y,z,cid in xyz:
    d[c].append((x,y,z,cid))

当我`打印(d)时给出这个:

defaultdict(<class 'list'>, {'L': [(32.992, 22.861, 29.486, 'TYR'), (32.613, 26.653, 29.569, 'VAL'), (30.029, 28.873, 27.872, 'LEU')],'H': [(30.254, 32.655, 27.849, 'THR'), (27.487, 35.089, 27.0, 'GLN'), (27.343, 38.9, 27.424, 'PRO')], 'A': [(25.621, 40.067, 30.641, 'PRO'), (23.161, 42.327, 28.82, 'SER'), (22.086, 43.358, 25.326, 'VAL'), (20.081, 46.519, 24.785, 'SER'), (18.23, 46.826, 21.488, 'VAL')]})
4

1 回答 1

2

我将在这里猜测你想要什么。

你有一本字典,像这样:

{'L': [(32.992, 22.861, 29.486, 'TYR'), (32.613, 26.653, 29.569, 'VAL'), (30.029, 28.873,     27.872, 'LEU')],
 'A': [(1.719, -25.217, 8.694, 'PRO'), (2.934, -21.997, 7.084, 'SER'), (5.35, -19.779, 8.986, 'VAL')],
 'H': [(-0.511, 19.577, 27.422, 'GLU'), (2.336, 18.416, 29.649, 'VAL'), (2.65, 19.35, 33.322, 'GLN')]}

你想要的是这样的伪代码:

for each list in the dictionary's values:
    for each of the other two lists:
        for each element in the first list:
            for each element in the other list:
                do something with the distance between the two elements.

在这种情况下,“其他两个列表中的每一个”都非常简单,因为只有 3 个……但总的来说,这样做更简单:

for each pair of lists in the dictionary's values:
    for each pair of elements from the cartesian product of the two lists:
        do something with the distance between the two elements

你可以直接把它翻译成 Python:

from itertools import permutations, product, chain

for lst1, lst2 in permutations(d.values(), 2):
    for e1, e2 in chain.from_iterable(product(lst1, lst2)):
        do_something_with(dist(e1, e2))

如果您只是想将这些距离收集到一个列表中作为理解,那很容易:

distances = [dist(e1, e2) for lst1, lst2 in permutations(d.values(), 2)
             for e1, e2 in chain.from_iterable(product(lst1, lst2))]

但是,我认为它可能更具可读性:

list_pairs = permutations(d.values(), 2)
item_pairs = chain.from_iterable(product(lst1, lst2) for lst1, lst2 in list_pairs)
distances = [dist(e1, e2) for e1, e2 in item_pairs]
于 2013-09-12T00:54:51.460 回答