如何从Python中的邻接列表中选择一个子矩阵? [英] How to select a submatrix from an adjacency list in Python?

查看:279
本文介绍了如何从Python中的邻接列表中选择一个子矩阵?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个邻接表,其中每个数组代表该行的非零列(例如,下面的adj。列表中的第0个数组意味着第2列和第6列为1,其他所有为0)。
$ b


adj_list = [[2,6],[1,3,24],[2,4],[3,5,21],[4, 6,10],[1,5,
7],[6,8,9],[7],[7,10,14],[5,9,11],[10,12, 18],[11,13],
[12,14,15],[9,13],[13,16,17],[15],[15],[11,19,20] ,[18],
[18],[4,22,23],[21],[21],[2,25,26],[24],[24]]

b $ b

鉴于此形容词。列表中,我想选择一个具有相同的行和列索引的子矩阵,如下所示:

  submatrix =(0, 1,2,5,22)

子矩阵中的每个元素表示一个行号。

<1>对于子矩阵中的每一行 i ,我需要得到 iith adj_list (这相当于从一个邻接矩阵中获取 ith 行) >

2)然后从该数组中,我需要提取与子矩阵匹配的项目。例如,如果我是当前查看子矩阵中的第3个元素,它是5,那么我需要在adj_list中获得第5个数组(相当于获得第5行的adj.matrix),即[ 1,5,7],然后我需要查看[1,5,7]中的哪些元素与子矩阵匹配(相当于获得第5行的第1,第5和第7列)。在这种情况下,第5行的结果应该是[0,1,0,1,0],因为只有1和5在两个数组中相交)。



我可以有效地选择这个submatrix给出的形容词。

24],[2,4],[3,5,21],[4,6,10],[1,5,7],[6,8,9],[7],[7,10, 14],[13],[16],[17],[15],[14],[15] ,[15],[11,19,20],[18],[18],[4,22,23],[21],[21],[2,25,26],[24],[ 24]]

submatrix =(0,1,2,5,22)

结果= [[i in adj_list [sm] for i in submatrix] for sm in子矩阵]

这应该做到这一点;尽管我怀疑如果你更仔细地考虑你的最终目标,你可能更喜欢计算除此之外的其他东西。


I have an adjacency list where each array represents non-zero columns at that row (e.g. 0th array in the adj. list below means columns 2 and 6 are 1, and everything else is 0).

adj_list = [[2, 6], [1, 3, 24], [2, 4], [3, 5, 21], [4, 6, 10], [1, 5, 7], [6, 8, 9], [7], [7, 10, 14], [5, 9, 11], [10, 12, 18], [11, 13], [12, 14, 15], [9, 13], [13, 16, 17], [15], [15], [11, 19, 20], [18], [18], [4, 22, 23], [21], [21], [2, 25, 26], [24], [24]]

Given this adj. list, I would like to select a submatrix which has identical row and column indices that is given by:

submatrix = (0, 1, 2, 5, 22)

Each element in submatrix indicates a row number.

1) For each row i in submatrix, I need to get ith array from adj_list (which is equivalent to getting ith row from an adjacency matrix)

2) Then from that array, I need to extract the items that match with submatrix

For example, if I am currently looking at 3rd element in submatrix, which is 5, then I need to go to 5th array in adj_list (equivalent of getting 5th row of adj.matrix), which is [1,5,7], and then I need to look which elements in [1,5,7] matches with submatrix (equivalent of getting 1th, 5th and 7th columns of the 5th row). In this case, the result for 5th row should be [0,1,0,1,0] because only 1 and 5 are intersected in two arrays).

How can I efficiently select this submatrix given the adj. list?

解决方案

adj_list = [[2, 6], [1, 3, 24], [2, 4], [3, 5, 21], [4, 6, 10], [1, 5, 7], [6, 8, 9], [7], [7, 10, 14], [5, 9, 11], [10, 12, 18], [11, 13], [12, 14, 15], [9, 13], [13, 16, 17], [15], [15], [11, 19, 20], [18], [18], [4, 22, 23], [21], [21], [2, 25, 26], [24], [24]]

submatrix = (0, 1, 2, 5, 22)

result = [[i in adj_list[sm] for i in submatrix] for sm in submatrix]

This should do it; though I suspect you might prefer to compute something other than this if you consider your end goal more carefully.

这篇关于如何从Python中的邻接列表中选择一个子矩阵?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆