Created
November 3, 2014 01:21
-
-
Save binki/556f2c90952739e7e3ae to your computer and use it in GitHub Desktop.
This file contains hidden or 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 is_int(x): | |
... try: | |
... int(x) | |
... return True | |
... except: | |
... pass | |
... return False | |
... | |
>>> [int(x) if is_int(x) else x for x in 'asdf asdf 23'.split(' ')] | |
['asdf', 'asdf', 23] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The exception handling should be made more specific to catch
ValueError
or something like that. So don’t use this gist as an example :-p.