forked from WilliamQLiu/python-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlist_comprehension_sample.py
More file actions
67 lines (53 loc) · 2.51 KB
/
list_comprehension_sample.py
File metadata and controls
67 lines (53 loc) · 2.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
""" Notes on how to do comprehensions in Python with reference to ...
* list comprehension: PEP 2020 - http://legacy.python.org/dev/peps/pep-0202/
* dict comprehension: http://legacy.python.org/dev/peps/pep-0274/
* simple generators: http://legacy.python.org/dev/peps/pep-0255/
* generator expressions: http://legacy.python.org/dev/peps/pep-0289/
"""
def list_comp_example():
""" PEP 202 - list comprehensions provide a more concise way to create lists
in situations where map() and filter() and/or nested loops is used """
print "Example 1:"
print [i for i in range(10)] # [0,1,2,3,4,5,6,7,8,9]
print "Example 2:"
print [i for i in range(20) if i%2==0] #[0,2,4,6,8,10,12,14,16,18]
def invert(d):
""" Used for dict_comp_example > Example 4; flips dict's keys and values """
return {v : k for k, v in d.iteritems()}
def dict_comp_example():
""" PEP 274 - similar to list comprehensions, but returns a dict objects
instead of list objects """
print "Example 1:"
print {i : chr(65+i) for i in range(4)} # {0:'A', 1:'B', 2:'C', 3:'D'}
print "Example 2:"
someDict = {0:'A', 1:'B', 2:'C'}
print {k : v for k, v in someDict.iteritems()} == someDict.copy() # True
print "Example 3:"
print {x.lower() : 1 for x in list_of_email_addrs}
#{'[email protected]': 1, '[email protected]': 1}
print "Example 4:"
d = {0:'A', 1:'B', 2:'C', 3:'D'}
print invert(d) # {'A':0, 'B':1, 'C':2, 'D':3}
print "Example 5:"
print {(k, v): k+v for k in range(4) for v in range(4)}
# {(0, 1): 1, (1, 2): 3, (3, 2): 5, (0, 0): 0, (3, 3): 6, (3, 0): 3,
# (3, 1): 4, (2, 1): 3, (0, 2): 2, (2, 0): 2, (1, 3): 4, (2, 3): 5,
# (2, 2): 4, (1, 0): 1, (0, 3): 3, (1, 1): 2}
def simple_generator_example():
""" PEP 255 - a generator is simply a function that returns an object on
which you can call next so that for every call it returns some value
until it raises a StopIteration exception"""
sum_of_first_n = sum(xrange(1000000))
print "Example 1:"
print sum_of_first_n # 499999500000
def generator_expressions():
""" PEP 289 - generator expressions are high performance, memory efficient
generalization of list comprehensions and generators """
print "Example 1:"
print sum(x*x for x in range(10)) #285
if __name__ == '__main__':
list_comp_example()
dict_comp_example()
simple_generator_example()
generator_expressions()