Created
December 8, 2014 21:20
-
-
Save BrianHicks/8b063def5f66aad46ff5 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 ints1(lst): | |
... return [int(x) for x in lst if x.isdigit()] | |
... | |
>>> def ints2(lst): | |
... out = [] | |
... for item in lst: | |
... try: | |
... out.append(int(item)) | |
... except ValueError: | |
... pass | |
... return out | |
... | |
>>> ints1(['a', '1']) | |
[1] | |
>>> ints2(['a', '1']) | |
[1] | |
>>> import timeit | |
>>> timeit.timeit(setup='from __main__ import *', stmt='ints1(["1", "2", "a", "b"])') | |
2.519864082336426 | |
>>> timeit.timeit(setup='from __main__ import *', stmt='ints2(["1", "2", "a", "b"])') | |
9.285669088363647 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment