假设我有一个字典,我使用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')]})
答案 0 :(得分: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]