为了账号安全,请及时绑定邮箱和手机立即绑定

在距离矩阵中查找值的索引?

在距离矩阵中查找值的索引?

翻过高山走不出你 2021-09-14 17:38:17
从 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”,是否可以将其添加到输出文本文件中?
查看完整描述

2 回答

?
慕后森

TA贡献1802条经验 获得超5个赞

根据评论,您可以使用 enumerate 这相当于


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 是您的索引


查看完整回答
反对 回复 2021-09-14
?
当年话下

TA贡献1890条经验 获得超9个赞

你可以使用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()

哈。


查看完整回答
反对 回复 2021-09-14
  • 2 回答
  • 0 关注
  • 221 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号