Last active
December 11, 2015 19:49
-
-
Save bennuttall/4651492 to your computer and use it in GitHub Desktop.
Comprehension in Python
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
my_dict = {i: i ** 2 for i in range(1, 11)} | |
-> {1: 1, 2: 4, 3: 9, 4: 16, 5: 25, 6: 36, 7: 49, 8: 64, 9: 81, 10: 100} | |
my_dict[7] | |
-> 49 |
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
gen = (i ** i for i in range(1, 10)) | |
print gen | |
-> <generator object <genexpr> at 0xfc7d20> | |
gen.next() | |
-> 1 | |
gen.next() | |
-> 4 | |
gen.next() | |
-> 27 | |
gen.next() | |
-> 256 |
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
rows = cols = range(3) | |
grid = [[(r, c) for c in cols] for r in rows] | |
-> [[(0, 0), (1, 0), (2, 0)], | |
[(0, 1), (1, 1), (2, 1)], | |
[(0, 2), (1, 2), (2, 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
msg = [['M', 'J', 'S', 'F'], ['A', 'O', 'O', 'O'], ['K', 'H', 'M', 'O'], ['E', 'N', 'E', 'D']] | |
' '.join(''.join(msg[j][i] for j in range(4)) for i in range(4)) | |
-> 'MAKE JOHN SOME FOOD' |
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
rows = cols = range(3) | |
grid = [] | |
for r in rows: | |
for c in cols: | |
if r != c: | |
cell = (r, c) | |
grid.append(cell) | |
-> [(1, 0), (2, 0), (0, 1), (2, 1), (0, 2), (1, 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
rows = cols = range(3) | |
grid = [(r, c) for c in cols for r in rows] | |
-> [(0, 0), (1, 0), (2, 0), (0, 1), (1, 1), (2, 1), (0, 2), (1, 2), (2, 2)] | |
grid = [(r, c) for c in cols for r in rows if r != c] | |
-> [(1, 0), (2, 0), (0, 1), (2, 1), (0, 2), (1, 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
my_list = [i % 3 for i in range(10)] | |
-> [0, 1, 2, 0, 1, 2, 0, 1, 2, 0] | |
my_set = {i % 3 for i in range(10)} | |
-> set([0, 1, 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
squares = [] | |
for n in range(10): | |
squares.append(n ** 2) | |
-> [0, 1, 4, 9, 16, 25, 36, 49, 64, 81] |
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
squares = [n ** 2 for n in range(10)] | |
-> [0, 1, 4, 9, 16, 25, 36, 49, 64, 81] |
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
char_list = [c for c in 'abracadabra'] | |
-> ['a', 'b', 'r', 'a', 'c', 'a', 'd', 'a', 'b', 'r', 'a'] | |
char_set = {c for c in 'abracadabra'} | |
-> set(['a', 'r', 'b', 'c', 'd']) |
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
sum(i ** 2 for i in range(10)) | |
-> 285 |
hughdbrown
commented
Jan 28, 2013
Well spotted - thanks for pointing it out. Now fixed!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment