简体   繁体   English

如何找到重复元素在列表中的位置,然后对这些元素求和?

[英]How to find the position of repeated elements in a list, then sum those elements?

I want to find the position of the repeated elements in the column "1" and the column "2" in a specified matrix "x" (the matrix "x" has a nx3 size ("n" is the maximum number between the column "1" and the column "2", in the next example is max(3,4)=4)), for then in the principal diagonal (number 1 to "n") of a new matrix "z"(the matrix "z" has a "n"x"n" size) sum the values for each column (except the columns with values equal to zero), and put the respective values off-diagonal in the respective column. 我想在指定的矩阵“ x”中找到列“ 1”和列“ 2”中重复元素的位置(矩阵“ x”的大小为nx3(“ n”是列之间的最大数目) “ 1”和列“ 2”,在下一个示例中是max(3,4)= 4)),然后在新矩阵“ z”(矩阵的主对角线(从1到n)中) “ z”的大小为“ n” x“ n”)将每列(值等于零的列除外)的值相加,并将相应的值放在对角线上。 This is an example of a matrix "x": 这是矩阵“ x”的示例:

        1 2 3
x=  1 [[1,2,4],
    2  [1,3,2],
    3  [2,3,1],
    4  [3,4,5]]

The matrix "z" is a symmetric matrix, meaning for example the element z[1,2] is equal to z[2,1] and the element z[1,3] is equal to z[3,1].This is the structure of the matrix "z": 矩阵“ z”是一个对称矩阵,例如,元素z [1,2]等于z [2,1],元素z [1,3]等于z [3,1]。是矩阵“ z”的结构:

z=  [[z[1],z[1 to 2],z[1 to 3],z[1 to 4]], 
     [z[2 to 1],z[2],z[2 to 3],z[2 to 4]], 
     [[z[3 to 1],z[3 to 2],z[3],z[3 to 4]],
     [[z[4 to 1],z[4 to 2],z[4 to 3],z[4]]

The elements in the diagonal principal( for this example are): 对角主体中的元素(在此示例中为):

 z[1]=z[1 to 2]+z[1 to 3]+z[1 to 4]
 z[2]=z[2 to 1]+z[2 to 3]+z[2 to 4]
 z[3]=z[3 to 1]+z[3 to 2]+z[3 to 4]
 z[4]=z[4 to 1]+z[4 to 2]+z[4 to 3]

The desired matrix (for this example) is: 所需的矩阵(对于此示例)为:

z=  [[4+2=6,4,2,0], 
     [4,4+1=5,1,0], 
     [2,1,2+1+5=8,5],
     [0,0,5,5]]

Note: The zeros in the matrix "z" represent if there aren´t connection with the other numbers. 注意:矩阵“ z”中的零表示其他数字之间是否没有联系。 In this example the number "4" has only connection with the number "3"(the value is 5). 在此示例中,数字“ 4”仅与数字“ 3”(值为5)有关。 Then the connection for the number 4 with the number 1 and number 2 are zero. 那么数字4与数字1和数字2的连接为零。

Thanks. 谢谢。

Your problem is clarified if you introduce graph-theoretic terminology. 如果引入图论术语,您的问题将得到澄清。 Your x could be thought of as describing a weighted graph. 您的x可以认为是描述加权图。 A row like [1,3,2] can be interpreted as describing the existence of an edge of weight 2 connecting node 1 and node 3. The matrix z contains the following information: 可以将类似于[1,3,2]行解释为描述连接节点1和节点3的权重2的边的存在。矩阵z包含以下信息:

1) For each node i , the corresponding diagonal entry is the sum of the weights of all edges which are incident with i . 1)对于每个节点i ,对应的对角线项是与i入射的所有边的权重之和。

2) For each pair of distinct nodes i and j , the corresponding entry of z is either 0 if the nodes are not connected, or the weight of the edge connecting i and j if they are (or sum of the weights if there are multiple edges). 2)对于每对不同的节点ij ,如果节点未连接,则z的对应条目为0如果它们连接,则连接ij的边的权重(如果存在多个,则权重之和)边缘)。

Given this interpretation, things are easy enough. 有了这种解释,事情就很容易了。 The following assumes that the nodes in x begin with 1 . 下面假设在节点x开始1 Note that the resulting matrix is 0-based even though the graph is 1-based, so to find the weight from node 2 to node 3 you will need z[1][2] : 请注意,即使图形是基于1的,所得矩阵也是基于0的,因此要找到从节点2到节点3的权重,您将需要z[1][2]

def makeMatrix(edges):
    n = max(max(edge[:2]) for edge in edges)
    z = [[0]*n for i in range(n)]
    for edge in edges:
        i,j,w = edge
        z[i-1][i-1] += w
        z[j-1][j-1] += w
        z[i-1][j-1] += w
        z[j-1][i-1] += w
    return z

The following helps for debugging: 以下有助于调试:

def pprint(matrix):
    for row in matrix:
        print(' '.join(str(i) for i in row))

For example, 例如,

>>> x = [[1,2,4],[1,3,2],[2,3,1],[3,4,5]]
>>> z = makeMatrix(x)
>>> pprint(z)
6 4 2 0
4 5 1 0
2 1 8 5
0 0 5 5

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM