Matrix list-to-list conversion doesn’t even work using numpy, how could I be doing this conversion?

Asked

Viewed 20 times

-1

import numpy as np

X = 10
matriz = []
for i in range(X):
    matriz.append([])

for l in range(0,X):
    for c in range(0,1):
        matriz[l].append(input().split())

print(matriz)
matriz_nova = np.array(matriz)

1 answer

1

That’s what you got

>>> X = 10
>>> matriz = []
>>> for i in range(X):
...     matriz.append([])
...
>>> for l in range(0, X):
...     for c in range(0,1):
...         matriz[l].append(input().split())
...
1 2 3 4 5 6 7 8 9 10
1 1 1 1 1 1 1 1 1 1
2 2 2 2 2 2 2 2 2 2
3 3 3 3 3 3 3 3 3 3
4 4 4 4 4 4 4 4 4 4
5 5 5 5 5 5 5 5 5 5
6 6 6 6 6 6 6 6 6 6
7 7 7 7 7 7 7 7 7 7
8 8 8 8 8 8 8 8 8 8
9 9 9 9 9 9 9 9 9 9
>>> print(matriz)
[[['1', '2', '3', '4', '5', '6', '7', '8', '9', '10']], [['1', '1', '1', '1', '1', '1', '1', '1', '1', '1']], [['2', '2', '2', '2', '2', '2', '2', '2', '2', '2']], [['3', '3', '3', '3', '3', '3', '3', '3', '3', '3']], [['4', '4', '4', '4', '4', '4', '4', '4', '4', '4']], [['5', '5', '5', '5', '5', '5', '5', '5', '5', '5']], [['6', '6', '6', '6', '6', '6', '6', '6', '6', '6']], [['7', '7', '7', '7', '7', '7', '7', '7', '7', '7']], [['8', '8', '8', '8', '8', '8', '8', '8', '8', '8']], [['9', '9', '9', '9', '9', '9', '9', '9', '9', '9']]]
>>> matriz_nova = np.array(matriz)
>>> matriz_nova
array([[['1', '2', '3', '4', '5', '6', '7', '8', '9', '10']],

       [['1', '1', '1', '1', '1', '1', '1', '1', '1', '1']],

       [['2', '2', '2', '2', '2', '2', '2', '2', '2', '2']],

       [['3', '3', '3', '3', '3', '3', '3', '3', '3', '3']],

       [['4', '4', '4', '4', '4', '4', '4', '4', '4', '4']],

       [['5', '5', '5', '5', '5', '5', '5', '5', '5', '5']],

       [['6', '6', '6', '6', '6', '6', '6', '6', '6', '6']],

       [['7', '7', '7', '7', '7', '7', '7', '7', '7', '7']],

       [['8', '8', '8', '8', '8', '8', '8', '8', '8', '8']],

       [['9', '9', '9', '9', '9', '9', '9', '9', '9', '9']]], dtype='<U2')

Change part of your code to

X = 10
matriz = [] 
for i in range(X): 
    matriz.append([])

for l in range(0, X): 
    for c in range(0,1): 
        matriz[l] += input().split()   # A ALTERAÇÃO FOI AQUI !!!

print(matriz) 
matriz_nova = np.array(matriz)

1 1 1 1 1 1 1 1 1 1
2 2 2 2 2 2 2 2 2 2
3 3 3 3 3 3 3 3 3 3
4 4 4 4 4 4 4 4 4 4
5 5 5 5 5 5 5 5 5 5
6 6 6 6 6 6 6 6 6 6
7 7 7 7 7 7 7 7 7 7
8 8 8 8 8 8 8 8 8 8
9 9 9 9 9 9 9 9 9 9
0 0 0 0 0 0 0 0 0 0
>>> print(matriz)
[['1', '1', '1', '1', '1', '1', '1', '1', '1', '1'], ['2', '2', '2', '2', '2', '2', '2', '2', '2', '2'], ['3', '3', '3', '3', '3', '3', '3', '3', '3', '3'], ['4', '4', '4', '4', '4', '4', '4', '4', '4', '4'], ['5', '5', '5', '5', '5', '5', '5', '5', '5', '5'], ['6', '6', '6', '6', '6', '6', '6', '6', '6', '6'], ['7', '7', '7', '7', '7', '7', '7', '7', '7', '7'], ['8', '8', '8', '8', '8', '8', '8', '8', '8', '8'], ['9', '9', '9', '9', '9', '9', '9', '9', '9', '9'], ['0', '0', '0', '0', '0', '0', '0', '0', '0', '0']]
>>> matriz_nova = np.array(matriz)
>>> matriz_nova
array([['1', '1', '1', '1', '1', '1', '1', '1', '1', '1'],
       ['2', '2', '2', '2', '2', '2', '2', '2', '2', '2'],
       ['3', '3', '3', '3', '3', '3', '3', '3', '3', '3'],
       ['4', '4', '4', '4', '4', '4', '4', '4', '4', '4'],
       ['5', '5', '5', '5', '5', '5', '5', '5', '5', '5'],
       ['6', '6', '6', '6', '6', '6', '6', '6', '6', '6'],
       ['7', '7', '7', '7', '7', '7', '7', '7', '7', '7'],
       ['8', '8', '8', '8', '8', '8', '8', '8', '8', '8'],
       ['9', '9', '9', '9', '9', '9', '9', '9', '9', '9'],
       ['0', '0', '0', '0', '0', '0', '0', '0', '0', '0']], dtype='<U1')

I hope it helps

Browser other questions tagged

You are not signed in. Login or sign up in order to post.