0

我创建了一个加权边缘列表,其中包含:

for key1, value1 in x1.items():
   for key2, value2 in x2.items():
      weight = 1 / (value1 + value2)
      row.append((key1, key2, str(weight)))

之后,列表列表包含重复项。例如:

[(a,a,0.5), ..., (a,b,0.2),..., (b,a,0.2), ..., (z,z,0.5)]

我的方法不起作用:

i = 0
j = 0
for row1 in matrix:
    for row2 in matrix:
        if(matrix[i][0] == matrix[j][1]):
           del(matrix[j])
        j +=1
    i +=1

我会得到这个按摩:

if(matrix[i][0] == matrix[j][1]):

IndexError: list index out of range

我究竟做错了什么?

4

1 回答 1

1

似乎j每次的值都可能归零。

i = 0
for row1 in matrix:
    j = 0
    for row2 in matrix:
        if(matrix[i][0] == matrix[j][1]):
           print(matrix[i][0], matrix[j][1])
        j +=1
    i +=1
于 2022-02-14T22:33:03.627 回答