Split Double Python Array

Asked

Viewed 206 times

0

I need to get coordenadas cartesianas and separate them so that you know what the ordered pair is. I receive a string where the dots come this way: x0,y0 x1,y1 ... xn,yn I used a array split by space first and then an array by commas, the problem is that the way I did is only storing the valor do primeiro par ordenado. I need to use while/for to traverse the entire array so that it doesn’t matter the amount of points typed Code:

pontos=vetor.split(' ')

while(cont<(2*len(pontos))):
    cord.append(0)
    cont=cont+1
cont=0
while(i<len(pontos)):
    p=(pontos[i].split(',')
        while(len(p)>cont):
           cord[cont]=p[cont]
           cord[cont+1]=p[cont+1]
           cord=cord+1
    i=i+1
    p=[]

2 answers

4


A more simplified way would be:

vetor = "0,3 4,5 6,7 10,4 0,3 5,3"

coord = [list(map(int,v.split(','))) for v in vetor.split()]

print(coord)
[[0, 3], [4, 5], [6, 7], [10, 4], [0, 3], [5, 3]]

1

coordenadas=[]
for i in range(5):
    for j in range(5):
        coordenadas.append([i,j])

print(coordenadas)


Browser other questions tagged

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