Created
February 11, 2011 03:04
-
-
Save pgollakota/821856 to your computer and use it in GitHub Desktop.
listsasstacks.py
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
>>> freshfruit = [' banana', ' loganberry ', 'passion fruit '] | |
>>> [weapon.strip() for weapon in freshfruit] | |
['banana', 'loganberry', 'passion fruit'] | |
>>> vec = [2, 4, 6] | |
>>> [3*x for x in vec] | |
[6, 12, 18] | |
>>> [3*x for x in vec if x > 3] | |
[12, 18] | |
>>> [3*x for x in vec if x < 2] | |
[] | |
>>> [[x,x**2] for x in vec] | |
[[2, 4], [4, 16], [6, 36]] | |
>>> [x, x**2 for x in vec] # error - parens required for tuples | |
File "<stdin>", line 1, in ? | |
[x, x**2 for x in vec] | |
^ | |
SyntaxError: invalid syntax | |
>>> [(x, x**2) for x in vec] | |
[(2, 4), (4, 16), (6, 36)] | |
>>> vec1 = [2, 4, 6] | |
>>> vec2 = [4, 3, -9] | |
>>> [x*y for x in vec1 for y in vec2] | |
[8, 6, -18, 16, 12, -36, 24, 18, -54] | |
>>> [x+y for x in vec1 for y in vec2] | |
[6, 5, -7, 8, 7, -5, 10, 9, -3] | |
>>> [vec1[i]*vec2[i] for i in range(len(vec1))] | |
[8, 12, -54] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment