生成集合列表,列表索引对应于列,集合包含寻址列的索引。
扫描行时,添加要设置的链接列索引。最后检查设定长度。
举个例子(为了避免出错,我使用了列名而不是索引):
sets[C1] = {C2}
sets[C5] = {C1,C3}
快速生成代码:
matrix = [[1,1,0,0,0],[0,0,1,0,0],[1,1,0,1,0],[0,0,1,0,1],[0,0,1,1,0]]
sets = []
for col in range(len(matrix[0])):
sets.append(set())
for row in range(len(matrix)):
leftone = -1
for col in range(len(matrix[row])):
if matrix[row][col] == 1:
if leftone < 0:
sets[col].add(col)
else:
sets[col].add(leftone)
leftone = col
for col in range(len(matrix[0])):
print(sets[col], len(sets[col]) == 1)
>>>
{0} True
{0} True
{2} True
{1, 2} False
{2} True