I have nxm
matrix and I want to find programatically N or more rows that contains non zero cells in more than M common columns.
For example. Here is the matrix:
matrix([[ 0., 0., 1., 1., 1., 0., 1., 0.],
[ 1., 0., 1., 0., 1., 1., 0., 1.],
[ 1., 0., 0., 0., 0., 1., 0., 0.],
[ 0., 0., 0., 0., 1., 0., 1., 0.],
[ 0., 1., 0., 1., 1., 0., 1., 0.],
[ 0., 1., 0., 1., 0., 0., 0., 0.]])
And I looking for 2 or more rows which contains non zero cells in 2 or more common colums. There are several possible results, one of them is:
row1: [ 1., 0., 1., 0., 1., 1., 0., 1.],
row2: [ 1., 0., 0., 0., 0., 1., 0., 0.],
col1 col5
Is it possible to find all rows combinations, that solve this task?
from itertools import combinations
matrix =[[ 0., 0., 1., 1., 1., 0., 1., 0.],
[ 1., 0., 1., 0., 1., 1., 0., 1.],
[ 1., 0., 0., 0., 0., 1., 0., 0.],
[ 0., 0., 0., 0., 1., 0., 1., 0.],
[ 0., 1., 0., 1., 1., 0., 1., 0.],
[ 0., 1., 0., 1., 1., 0., 0., 0.],
[ 0., 1., 0., 1., 1., 0., 0., 1.]]
m = 2
n = 6
req_rows = []
ncm = [x for x in combinations(matrix,m)]
for x in ncm:
if sum([1 for l in zip(*x) if not 0 in l])>=m:
print x
output
([0.0, 1.0, 0.0, 1.0, 1.0, 0.0, 0.0, 0.0], [0.0, 1.0, 0.0, 1.0, 1.0, 0.0, 0.0, 1.0])
([0.0, 0.0, 1.0, 1.0, 1.0, 0.0, 1.0, 0.0], [1.0, 0.0, 1.0, 0.0, 1.0, 1.0, 0.0, 1.0])
([0.0, 0.0, 1.0, 1.0, 1.0, 0.0, 1.0, 0.0], [0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 1.0, 0.0])
([0.0, 0.0, 1.0, 1.0, 1.0, 0.0, 1.0, 0.0], [0.0, 1.0, 0.0, 1.0, 1.0, 0.0, 1.0, 0.0])
([0.0, 0.0, 1.0, 1.0, 1.0, 0.0, 1.0, 0.0], [0.0, 1.0, 0.0, 1.0, 1.0, 0.0, 0.0, 0.0])
([0.0, 0.0, 1.0, 1.0, 1.0, 0.0, 1.0, 0.0], [0.0, 1.0, 0.0, 1.0, 1.0, 0.0, 0.0, 1.0])
([1.0, 0.0, 1.0, 0.0, 1.0, 1.0, 0.0, 1.0], [1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0])
([1.0, 0.0, 1.0, 0.0, 1.0, 1.0, 0.0, 1.0], [0.0, 1.0, 0.0, 1.0, 1.0, 0.0, 0.0, 1.0])
([0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 1.0, 0.0], [0.0, 1.0, 0.0, 1.0, 1.0, 0.0, 1.0, 0.0])
([0.0, 1.0, 0.0, 1.0, 1.0, 0.0, 1.0, 0.0], [0.0, 1.0, 0.0, 1.0, 1.0, 0.0, 0.0, 0.0])
([0.0, 1.0, 0.0, 1.0, 1.0, 0.0, 1.0, 0.0], [0.0, 1.0, 0.0, 1.0, 1.0, 0.0, 0.0, 1.0])
([0.0, 1.0, 0.0, 1.0, 1.0, 0.0, 0.0, 0.0], [0.0, 1.0, 0.0, 1.0, 1.0, 0.0, 0.0, 1.0])
from pprint import pprint
from itertools import combinations
def solve(lst, m):
col, n = {}, len(lst)
for i, x in enumerate(lst):
col[i] = [j for j, y in enumerate(x) if y]
for s in xrange(n, m-1, -1):
for c in combinations(xrange(n), s):
values = set(col[c[0]]).intersection(*(col[k] for k in c[1:]))
if len(values) >= m:
yield [lst[k] for k in c]
for x in solve(matrix, 2):
pprint(x)
Output:
[[0, 0, 1, 1, 1, 0, 1, 0],
[0, 0, 0, 0, 1, 0, 1, 0],
[0, 1, 0, 1, 1, 0, 1, 0]]
[[0, 0, 1, 1, 1, 0, 1, 0], [1, 0, 1, 0, 1, 1, 0, 1]]
[[0, 0, 1, 1, 1, 0, 1, 0], [0, 0, 0, 0, 1, 0, 1, 0]]
[[0, 0, 1, 1, 1, 0, 1, 0], [0, 1, 0, 1, 1, 0, 1, 0]]
[[1, 0, 1, 0, 1, 1, 0, 1], [1, 0, 0, 0, 0, 1, 0, 0]]
[[0, 0, 0, 0, 1, 0, 1, 0], [0, 1, 0, 1, 1, 0, 1, 0]]
[[0, 1, 0, 1, 1, 0, 1, 0], [0, 1, 0, 1, 0, 0, 0, 0]]
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.