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 f(list_of_lists): | |
| sets = {} | |
| for lst in list_of_lists: | |
| s = set(lst) | |
| for x in set(lst): | |
| if x in sets: | |
| t = sets[x] | |
| for y in t: | |
| sets[y] = s |
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
| import random | |
| import timeit | |
| def inter_reduce(sets): | |
| return reduce(set.intersection, sets) | |
| def inter_for(sets): | |
| sets = iter(sets) | |
| result = next(sets) | |
| for s in sets: |
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
| In [1]: a = array.array("i", range(100000)) | |
| In [2]: b = array.array("i", range(100000)) | |
| In [3]: %timeit for i, x in enumerate(a): b[i] = 2 * x | |
| 10 loops, best of 3: 20.6 ms per loop | |
| In [4]: a = range(100000) | |
| In [5]: b = range(100000) |
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
| >>> class A(object): | |
| ... i = 3 | |
| ... a = [i for x in range(3)] | |
| ... | |
| >>> class A(object): | |
| ... i = 3 | |
| ... a = list(i for x in range(3)) | |
| ... | |
| Traceback (most recent call last): | |
| File "<stdin>", line 1, in <module> |
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
| In [1]: import numpy | |
| In [2]: a = numpy.random.randint(100, size=100) | |
| In [3]: ((25 < a) & (a < 100)).sum() | |
| Out[3]: 72 |
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
| class MyRECache(dict): | |
| def __missing__(self, regex): | |
| return self.setdefault(regex, re.compile(regex)) |
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
| import timeit | |
| import numpy | |
| import scipy.ndimage.morphology | |
| def f0(a, kernel): | |
| b = numpy.convolve(a, kernel, mode="same") > 1 | |
| b[1:] |= b[:-1] | |
| b[:-1] |= b[1:] | |
| return b |
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
| In [1]: import numpy | |
| In [2]: a = numpy.array([0.5, 1.3, 2.4, 10.3, 10.8, 10.2, 7.6, 3.2, 2.9]) | |
| In [3]: numpy.unwrap(2.0 * a) / 2.0 | |
| Out[3]: | |
| array([ 0.5 , 1.3 , 2.4 , 0.87522204, 1.37522204, | |
| 0.77522204, 1.31681469, 0.05840735, -0.24159265]) |
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
| class A(object): | |
| def __init__(self, i): | |
| self.i = i | |
| def __radd__(self, other): | |
| return self.i | |
| a = [A(42)] | |
| # Test with built-in sum() | |
| print(sum(a)) |
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
| >>> import ctypes | |
| >>> buffer = (ctypes.c_char * 5)(*"abcde") | |
| >>> ctypes.cast(buffer, ctypes.c_char_p) | |
| c_char_p(3073075392) |