2 回答
TA贡献1772条经验 获得超6个赞
这似乎可以解决问题:
d_1 = {(1, 2): 0,
(1, 3): 0,
(1, 4): 0,
(1, 5): 0,
(2, 1): 0,
(2, 3): 0,
(2, 4): 0,
(2, 5): 0,
(3, 1): 0,
(3, 2): 0,
(3, 4): 0,
(3, 5): 0,
(4, 1): 0,
(4, 2): 0,
(4, 3): 0,
(4, 5): 0,
(5, 1): 0,
(5, 2): 0,
(5, 3): 0,
(5, 4): 0}
new_keys = []
for k in d_1:
invert = (k[1], k[0])
if invert not in new_keys:
new_keys.append(k)
d_2 = {}
for k in new_keys:
d_2[k] = d_1[k]
df = [
[1, 1, 1, 0, 0],
[1, 1, 0, 0, 0],
[1, 0, 0, 1, 1],
[1, 1, 0, 1, 0],
[0, 1, 1, 0, 0],
]
d_3 = {}
for k in d_2:
v = 0
c1, c2 = k[0] - 1, k[1] - 1
for line in df:
if line[c1] == line[c2]:
v += 1
d_3[k] = v
print(d_3)
输出:
{(1, 2): 3, (1, 3): 1, (1, 4): 3, (1, 5): 2, (2, 3): 3, (2, 4): 1, (2, 5): 0, (3, 4): 1, (3, 5): 2, (4, 5): 4}
(看来您的示例至少有一个错误:您的结果(1,4)应该为3,而不是2,因为第2、3和4行与第1和4列匹配。)
添加回答
举报