Skip to content

Instantly share code, notes, and snippets.

@binki
Created November 3, 2014 01:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save binki/556f2c90952739e7e3ae to your computer and use it in GitHub Desktop.
Save binki/556f2c90952739e7e3ae to your computer and use it in GitHub Desktop.
>>> 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]
@binki
Copy link
Author

binki commented Nov 7, 2014

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment