从461个X,Y和Z坐标列表中,我使用SciPy创建了一个看起来像这样的距离矩阵
[[ 0. 3.78112691 6.55159315 ... 63.40661118 62.2923149
64.71125443]
[ 3.78112691 0. 3.76986434 ... 60.79913069 59.55251531
61.87364432]
[ 6.55159315 3.76986434 0. ... 61.12392086 59.65959803
61.94572052]
...
[63.40661118 60.79913069 61.12392086 ... 0. 3.8003808
5.63044026]
[62.2923149 59.55251531 59.65959803 ... 3.8003808 0.
3.82889361]
[64.71125443 61.87364432 61.94572052 ... 5.63044026 3.82889361
0. ]]
我还编写了代码,允许用户从距离矩阵中提取小于或等于某个值的值,然后将其写入文本文件
radius = int(input("Enter the radius threshold for contact (Angstrom): "))
###########This code will extract every pair <= radius##################
f = open('contact.txt', 'w')
for i in distance_matrix:
for j in i:
if j <= radius:
if j != 0:
f.write(str(j) + '\n')
print("File contact.txt written")
#########################################################################
文本文件(contact.txt)只是一长串值<=到用户指定的值。有没有办法写这些值来自哪对索引?例如,如果在x轴的索引b和y轴的索引c的交点处找到值“ a”,是否可以将其添加到输出文本文件中?
答案 0 :(得分:0)
根据评论,您可以使用枚举,等效于
zip( range( len(vec)), vec)
为此:
f = open('contact.txt', 'w')
for n1, i in enumerate(distance_matrix):
for n2 ,j in enumerate(i):
if j <= radius:
if j != 0:
f.write(str(j) + '\n')
print("File contact.txt written")
其中n1和n2是您的索引
答案 1 :(得分:0)
您可以使用np.where
indices = np.where((distance_matrix <= radius) & (distance_matrix != 0))
row_col_vals = list(zip(*indices, distance_matrix[indices])) # gives you a list or (row, col, value) tuples
例如,如果radius = 5
和
distance_matrix = np.array([[2, 6], [0, 3]])
然后
row_col_vals = [(0, 0, 2), (1, 1, 3)]
要写入文件:
f = open('contact.txt', 'w')
for row, col, val in row_col_vals:
print(row, col, val, file=f)
f.close()
HTH。