Compare duplicate items in a list of weighted edge

34 Views Asked by At

I created a weighted edge list of lists with:

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

After that the list of lists contain duplicates. For example:

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

My approach doesn't work:

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

I'll get this massage:

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

IndexError: list index out of range

What am I doing wrong?

1

There are 1 best solutions below

0
AziMez On BEST ANSWER

It seems that the value of j might be returned to zero every time.

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