string replacement in python -


i have string of form:

"77.1234 15.3456,79.8765 15.6789,81.9876 16.0011" 

i need convert form:

[[77.1234,15.3456],[79.8765,15.6789],[81.9876,16.0011]] 

can me accomplish using replace() function in python? if cannot achieved that, other function provided python can used?

thank you.

use list comprehension,

first split string comma, split each element of new list space , cast float type.

>>> x = "77.1234 15.3456,79.8765 15.6789,81.9876 16.0011" >>> [[float(j) j in i.split()] in x.split(',')] [[77.1234, 15.3456], [79.8765, 15.6789], [81.9876, 16.0011]] 

it achieves same result this:

>>> x = "77.1234 15.3456,79.8765 15.6789,81.9876 16.0011" >>> ls = [] >>> in x.split(','): ...     j in i.split(): ...             ls.append(float(j)) ...  >>> ls [77.1234, 15.3456, 79.8765, 15.6789, 81.9876, 16.0011] 

Comments

Popular posts from this blog

android - Get AccessToken using signpost OAuth without opening a browser (Two legged Oauth) -

org.mockito.exceptions.misusing.InvalidUseOfMatchersException: mockito -

google shop client API returns 400 bad request error while adding an item -