比较加权边列表中的重复项目
Compare duplicate items in a list of weighted edge
我创建了一个列表的加权边缘列表:
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
我做错了什么?
好像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
我创建了一个列表的加权边缘列表:
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
我做错了什么?
好像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