Last active
May 30, 2021 07:19
-
-
Save pengelana/d5a367b5a6b4f6fd1c2d90eb8a962edb to your computer and use it in GitHub Desktop.
dict 2
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
In [132]: def test1(): | |
...: with open('domain.txt') as f: | |
...: data = {} | |
...: for x in f: | |
...: length = len(x.strip().split('.')) | |
...: data[length] = 1 if not length in data else data[length] + 1 | |
...: return data | |
...: | |
In [133]: def test(): | |
...: with open('domain.txt') as f: | |
...: data = {} | |
...: for x in f: | |
...: length = len(x.strip().split('.')) | |
...: data[length] = data.get(length, 0) + 1 | |
...: return data | |
...: | |
In [169]: def test2(): | |
...: with open('domain.txt') as f: | |
...: data = {} | |
...: for x in f: | |
...: length = len(x.strip().split('.')) | |
...: if length in data: | |
...: data[length] = data[length] + 1 | |
...: else: data[length] = 1 | |
...: return data | |
...: | |
In [114]: def test3(): | |
...: with open('domain.txt') as f: | |
...: data = {} | |
...: for x in f: | |
...: if '#' in x: continue | |
...: length = len(x.strip().split('.')) | |
...: data.setdefault(length, []).append(x.strip()) | |
...: return data | |
...: | |
In [135]: test() | |
Out[135]: | |
{2: 105390, | |
3: 33938, | |
4: 4982, | |
5: 892, | |
6: 405, | |
7: 80, | |
8: 54, | |
9: 27, | |
10: 17, | |
11: 24, | |
12: 6, | |
13: 9, | |
14: 5, | |
15: 4, | |
16: 4, | |
17: 4, | |
18: 1, | |
19: 1} | |
In [136]: test1() | |
Out[136]: | |
{2: 105390, | |
3: 33938, | |
4: 4982, | |
5: 892, | |
6: 405, | |
7: 80, | |
8: 54, | |
9: 27, | |
10: 17, | |
11: 24, | |
12: 6, | |
13: 9, | |
14: 5, | |
15: 4, | |
16: 4, | |
17: 4, | |
18: 1, | |
19: 1} | |
In [169]: test2() | |
Out[169]: | |
{2: 105390, | |
3: 33938, | |
4: 4982, | |
5: 892, | |
6: 405, | |
7: 80, | |
8: 54, | |
9: 27, | |
10: 17, | |
11: 24, | |
12: 6, | |
13: 9, | |
14: 5, | |
15: 4, | |
16: 4, | |
17: 4, | |
18: 1, | |
19: 1} | |
In [139]: %timeit test() | |
10 loops, best of 3: 174 ms per loop | |
In [140]: %timeit test1() | |
10 loops, best of 3: 179 ms per loop | |
In [141]: %timeit test() | |
10 loops, best of 3: 172 ms per loop | |
In [142]: %timeit test1() | |
10 loops, best of 3: 170 ms per loop | |
In [143]: %timeit test() | |
10 loops, best of 3: 172 ms per loop | |
In [144]: %timeit test1() | |
10 loops, best of 3: 167 ms per loop | |
In [146]: %timeit test() | |
10 loops, best of 3: 171 ms per loop | |
In [147]: %timeit test1() | |
10 loops, best of 3: 163 ms per loop | |
In [148]: %timeit test() | |
10 loops, best of 3: 173 ms per loop | |
In [149]: %timeit test1() | |
10 loops, best of 3: 163 ms per loop | |
In [170]: %timeit test2() | |
10 loops, best of 3: 163 ms per loop | |
In [171]: %timeit test2() | |
10 loops, best of 3: 162 ms per loop | |
In [172]: %timeit test2() | |
10 loops, best of 3: 162 ms per loop | |
In [173]: %timeit test2() | |
10 loops, best of 3: 167 ms per loop | |
In [174]: %timeit test2() | |
10 loops, best of 3: 161 ms per loop | |
In [175]: %timeit test2() | |
10 loops, best of 3: 165 ms per loop |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment