Created
April 7, 2013 17:38
-
-
Save sbaer/5331496 to your computer and use it in GitHub Desktop.
Parse a string into a list of numbers
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from itertools import * | |
def tonumberlist(s): | |
rc = [] | |
def shouldtake(x): | |
return str.isdigit(x) or x=='.' | |
i = iter(s.replace(' ','')) | |
while True: | |
try: | |
rc.append(float(''.join(takewhile(shouldtake, i)))) | |
except ValueError: break | |
return rc | |
s = "12.1, 5; 66.32,123" | |
print tonumberlist(s) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment