How to join 2 lists in a single Matrix?

0

Make a program that creates two vectors, using lists, and receives 6 integers in each of the vectors. Finally, the program should create a 3x4 matrix from the interleaving of the two vectors.

This merge will occur as follows:

Capture two elements of vector1 and place in array

Capture two elements of vector2 and place in array

Repeat until you populate the array

How do I do this merge?

    vet1 = []
    vet2 = []
    vet3 = []

    print ('Informe os valores do primeiro vetor')

    for i in range(0, 3):
        vet1.append(int(input('Informe um numero: ')))

    print ('Informe os valores do segundo vetor')

    for i in range(0, 3):
        vet2.append(int(input('Informe um numero: ')))

    for i in range(0, 3):
        vet3.append(vet1[i]) 
        vet3.append(vet2[i])

     print (vet3)

This was the code I made for testing

    
asked by anonymous 12.11.2017 / 03:56

1 answer

0

I think this code can help you:

def intercalacao(vetor_1=None, vetor_2=None):
    num_linhas = 3
    num_colunas = 4
    # cria matriz 3x4
    matriz = [[0 for i in range(num_colunas)] for j in range(num_linhas)]

    # intercala os valores dos vetores
    # montando assim a matriz 
    for i in range(num_linhas):
        for j in range(num_colunas):
            # j for divisivel por 2
            if j % 2 == 0:
                # remove o primeiro elemento do vetor
                # e a posicao da matriz recebe seu valor
                matriz[i][j] = vet_1.pop(0)
                #
            else:
                matriz[i][j] = vet_2.pop(0)

    return matriz


if __name__ == '__main__':

    vet_1 = [1, 2, 3, 4, 5, 6]
    vet_2 = [7, 8, 9, 10, 11, 12]

    matriz = intercalacao(vetor_1=vet_1, vetor_2=vet_2)
    print(matriz)
    
13.11.2017 / 08:46