Transform Pandas str objects into numeric values

Asked

Viewed 45 times

0

How do I use Python to transform the following string:

[[98 9] [55 16] [9 50] [68 0] [24 1] [80 16]]

in two numerical vector chains (the first string containing the first values:

98 55 9 ...

and the second vector containing the second values:

9, 16, 50 ...

1 answer

1

This "string" that refers is actually a list of lists, or can be interpreted as an array as well, to do what you ask, just go through the list, and assign every value in the Dice '0' of each item, in a list, and the index '1' in another, as follows:

lista = [[98, 9], [55, 16], [9, 50], [68, 0], [24, 1], [80, 16]]
a = [] 
b = [] 
for i in lista:
        a.append(i[0])
        b.append(i[1]) 
print(a,'/',b)

The exit will be:

[98, 55, 9, 68, 24, 80] / [9, 16, 50, 0, 1, 16]

  • Thank you very much Absolve, but I’m still not getting it... when I give one print(type(lista) is seeming the following message: <class 'str'> I may have to do some conversion before to apply this solution. If you can help me in this conversion, I would be very grateful. Thank you very much!

  • There is probably a way to generate this output in list, or matrix format, however if you want to transform exactly the way it is in separate lists, work with list(string.split(" ")), so you partition the list using a key, in this case a comma or spaces.

Browser other questions tagged

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