Created
December 3, 2017 15:18
-
-
Save milo0/fae12f5d29294bc7bef72d6daef8d29c to your computer and use it in GitHub Desktop.
Extract numeric values from a string
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
def extract_integers(string): | |
extracted_ints = [int(s) for s in string.split() if s.isdigit()] | |
return extracted_ints | |
def extract_floats(string): | |
floats = [] | |
for s in string.split(): | |
try: | |
extracted_floats.append(float(s)) | |
except ValueError: | |
pass | |
return extracted_floats |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment