0

I have almost got it right but have an issue in getting my loop correct.....

There are three collector lists which are a list of sets.

list_1 = [(1, 1), (2, 2), (3, 3), (4, 4), (5, 5), (6, 6), (7, 7), (8, 8), (9, 9), (10, 10), (11, 11), (12, 12)]

list_2 = [(1, 1), (2, 7), (3, 3), (4, 4), (5, 2), (6, 5), (7, 8), (8, 2), (9, 9), (10, 1), (11, 3), (12, 4)]

list_3 = [(1, 7), (2, 8), (3, 3), (4, 1), (5, 2), (6, 4), (7, 5), (8, 12), (9, 11), (10, 10), (11, 9), (12, 6)]

My aim is to have this list in a form of a matrix with default three columns and rows being divisible by 3.

mat_1 = [[(1,1), (2,2), (3,3)],
         [(4,4), (5,5), (6,6)],
         [(7,7), (8,8), (9,9)],
         [(10,10), (11,11), (12,12)]]
                        
mat_2 = [[(1,1), (2,7), (3,3)],
         [(4,4), (5,2), (6,5)],
         [(7,8), (8,2), (9,9)],
         [(10,1), (11,3), (12,4)]]

mat_3 = [[(1,7), (2,8), (3,3)],
         [(4,1), (5,2), (6,4)],
         [(7,5), (8,12), (9,11)],
         [(10,10), (11,9), (12,6)]]

Here's my code,

list_1 = [(1, 1), (2, 2), (3, 3), (4, 4), (5, 5), (6, 6), (7, 7), (8, 8), (9, 9), (10, 10), (11, 11), (12, 12)]

list_2 = [(1, 1), (2, 7), (3, 3), (4, 4), (5, 2), (6, 5), (7, 8), (8, 2), (9, 9), (10, 1), (11, 3), (12, 4)]

list_3 = [(1, 7), (2, 8), (3, 3), (4, 1), (5, 2), (6, 4), (7, 5), (8, 12), (9, 11), (10, 10), (11, 9), (12, 6)]

n = len(list_1) # length of the list
mat_len = int(n/3) #  length of the matrix

mat_1 = []
mat_2 = []
mat_3 = []

Here's my function which makes the required matrix,

def make_matrix(mat_len, mat, lst):
    
    # mat_len = length of the matrix
    # mat = the required matrix to be created - mat_1, mat_2 & mat_3 respectively
    # lst = the input values from - list_1, list_2 & list_3 respectively
    
    mat = []
    
    for i in range(mat_len):
        mat.append([])
    
    i = 0
    while i in range(mat_len):
        j = 0
        while j in range(mat_len - 1):
            if i%2 == 0:
                mat[i].append(lst[j])
            else:
                mat[i].append(lst[j+(mat_len-1)])
            j = j + 1
        i = i + 1
    
    return mat

Here's how the output is known,

mat_1 = make_matrix(mat_len, mat_1, list_1)
mat_2 = make_matrix(mat_len, mat_2, list_2)
mat_3 = make_matrix(mat_len, mat_3, list_3)

Here's what I have achieved till,

Matrix 1,

[[(1, 1), (2, 2), (3, 3)],
 [(4, 4), (5, 5), (6, 6)],
 [(1, 1), (2, 2), (3, 3)],
 [(4, 4), (5, 5), (6, 6)]]

Matrix 2,

[[(1, 1), (2, 7), (3, 3)],
 [(4, 4), (5, 2), (6, 5)],
 [(1, 1), (2, 7), (3, 3)],
 [(4, 4), (5, 2), (6, 5)]]

Matrix 3,

[[(1, 7), (2, 8), (3, 3)],
 [(4, 1), (5, 2), (6, 4)],
 [(1, 7), (2, 8), (3, 3)],
 [(4, 1), (5, 2), (6, 4)]]

So, my issue is that the values in each row are alternatively repeating. I know that there is an issue in the function - make_matrix. But I am not able to solve that issue.

It would be very helpful if this issue can be solved.

Thanks

0 Answers0