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
Post a Comment