简体   繁体   中英

Python: Test to check proper order of boolean elements in a list of lists

My approach to this problem may be a little intense on the for loops and if statements. So, I have this data, provided below. The data solutions is a list of lists with 1's or 0's. There is one 6x6 list in solutions that is the correct solution. I actually solved this manually and found it to be the fifth list in solutions (python index solutions[4] ). BUT I CANNOT ASSUME THAT I KNOW THIS! After a solutions list is transposed, each element in the transposed list is supposed to have a number of 1's reflecting the corresponding numbers in the x data, provided below. Also, there HAS to be a 0 in between each group of 1('s). For example, x[2] = [2, 1, 1] . This would mean that in np.transpose(solutions[p])[2] the list would have to be [1,1,0,1,0,1] .

So, the code below is an attempt at writing something that checks for the proper spacing and order of each line in every solutions in accordance with x .

final_solutions = []
for p in range(0,len(solutions)):
    sol_trans = np.transpose(solutions[p])
    for q in range(0,len(x)):
        count = 0
        for r in range(0,len(sol_trans[q])):
            if sol_trans[q][r] == 0:
                isok = True
            if sol_trans[q][r] == 1:
                if sol_trans[q][r+x[q][count]] != 1:
                    r += x[q][count]-1
                    count += 1
                    isok = True
                else:
                    isok = False
                    break
    if isok == True:
        final_solutions.append(solutions[p])

I'll give you some sample data below.

x
Out: [[1], [4], [2, 1, 1], [1, 1, 1], [4], [1, 1]]

As an example of a failure of the test, solutions[0] would fail because np.transpose(solutions[p])[1] has a break in the 1's when there should have been four consecutive 1's. It would also fail because np.transpose(solutions[p])[2] leaves no space between entries of 1's. there are four consecutive 1's when there should be two ones, followed ya space and a single 1, then followed by another 1. It would also fail because np.transpose(solutions[p])[3] needs three single entries of 1's spaced apart. However, the first two are not spaced apart.

solutions
Out: 
[([0, 0, 0, 1, 0, 1],
 [0, 1, 1, 1, 1, 0],
 [0, 0, 1, 0, 1, 0],
 [0, 1, 1, 1, 1, 0],
 [0, 1, 1, 0, 1, 1],
 [1, 1, 0, 0, 0, 0]),
([0, 0, 0, 1, 0, 1],
 [0, 1, 1, 1, 1, 0],
 [0, 0, 1, 0, 1, 0],
 [0, 1, 1, 1, 1, 0],
 [1, 1, 0, 0, 1, 1],
 [0, 1, 1, 0, 0, 0]),
([0, 0, 0, 1, 0, 1],
 [0, 1, 1, 1, 1, 0],
 [1, 0, 0, 0, 1, 0],
 [0, 1, 1, 1, 1, 0],
 [0, 1, 1, 0, 1, 1],
 [0, 1, 1, 0, 0, 0]),
([0, 0, 1, 0, 0, 1],
 [0, 1, 1, 1, 1, 0],
 [0, 1, 0, 0, 1, 0],
 [0, 0, 1, 1, 1, 1],
 [1, 1, 0, 1, 1, 0],
 [0, 1, 1, 0, 0, 0]),
([0, 0, 1, 0, 0, 1],
 [0, 1, 1, 1, 1, 0],
 [0, 1, 0, 0, 1, 0],
 [0, 1, 1, 1, 1, 0],
 [1, 1, 0, 0, 1, 1],
 [0, 0, 1, 1, 0, 0]),
([0, 0, 1, 0, 0, 1],
 [1, 1, 1, 1, 0, 0],
 [0, 1, 0, 0, 1, 0],
 [0, 1, 1, 1, 1, 0],
 [0, 1, 1, 0, 1, 1],
 [0, 0, 0, 1, 1, 0]),
([0, 0, 1, 0, 1, 0],
 [0, 1, 1, 1, 1, 0],
 [0, 0, 0, 1, 0, 1],
 [0, 1, 1, 1, 1, 0],
 [0, 1, 1, 0, 1, 1],
 [1, 1, 0, 0, 0, 0]),
([0, 0, 1, 0, 1, 0],
 [0, 1, 1, 1, 1, 0],
 [0, 0, 0, 1, 0, 1],
 [0, 1, 1, 1, 1, 0],
 [1, 1, 0, 0, 1, 1],
 [0, 1, 1, 0, 0, 0]),
([0, 0, 1, 0, 1, 0],
 [0, 1, 1, 1, 1, 0],
 [0, 1, 0, 0, 0, 1],
 [0, 0, 1, 1, 1, 1],
 [1, 1, 0, 1, 1, 0],
 [0, 1, 1, 0, 0, 0]),

Does anyone have any idea of how to fix this broken code? Or maybe they have solved a similar problem, or know of an intuitive way to handle this that I just don't see.

Thanks for making it this far! This code is the last part of a puzzle that a friend gave me.

Here is a solution. I copy pasted your solutions as list literals, so I had to convert mine to numpy matrices first.

for si, solution in enumerate(solutions):
    solution = np.matrix(solution)
    matched_solution = True
    tran_solution = np.transpose(solution)
    for i, row_x in enumerate(x):
        row = tran_solution[i]
        row_x_calc = []
        cur = 0
        for val in row.flat:
            if val:
                cur += 1
            elif cur:
                row_x_calc.append(cur)
                cur = 0
        else:
            if cur:
                row_x_calc.append(cur)
        if row_x != row_x_calc:
            matched_solution = False
            break
    if matched_solution:
        print 'Found', si
        print solution
        break

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM