the problem that I am facing is simple. I have matrix as list of lists like this:
[[0, 0, 1, 0, 0, 0, 0], [0, 1, 0, 1, 0, 0, 1], [1, 1, 1, 1, 1, 1, 1], [0, 0, 1, 1, 0, 0, 1], [0, 0, 0, 1, 1, 1, 0], [0, 0, 1, 0, 1, 0, 0]]
And I want to get is tuple that has grouped occurrences of 1 for rows on one side and for cols on the other. So for the matrix above it would be like this:
([[1], [1, 1, 1], [7], [2, 1], [3], [1, 1]],[[1], [2], [1, 2, 1], [4], [1, 2], [1, 1], [3]])
As you can see it "sums" adjascent duplicates so list like this [1,0,1,1] would resolve in [[1],[0],[2]]
I tried working on the rows and ended up with this:
for line in matrix:
new_list = []
for value in line:
if new_list and new_list[-1][0] == value:
new_list[-1].append(value)
else:
new_list.append([value])
result_list.append(new_list)
This splits the list into duplicates but does not sum the adjascent and still leaves 0 in the result.
Does anyone have good solution for both rows and colls to achieve my desired Tuple?
PS: I know i could use itertools.groupby but I am trying to solve it without it.
Source: Python Questions