-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlist comprehension.py
More file actions
138 lines (100 loc) · 2.92 KB
/
Copy pathlist comprehension.py
File metadata and controls
138 lines (100 loc) · 2.92 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
print
# 1 - Intro
def filter(x):
return x >= 'B'
def expressions(x):
return '(%s)' % x
alpha_list = ['A','B','C']
print alpha_list
new_list = []
for i in alpha_list:
if filter(i):
new_list.append(expressions(i))
print new_list
# Read statements, left to right
print [expressions(i) for i in alpha_list if filter(i)]
# 2 - Chained vs Embeded
num_list = [20,40,60]
print ['%s-%d' % (x, y) for x in alpha_list for y in num_list]
out_list = []
for x in alpha_list:
for y in num_list:
out_list.append('%s-%d' % (x, y))
print out_list
print [['%s-%d' % (x, y) for x in alpha_list] for y in num_list]
out_list = []
for y in num_list:
out_list_inner = []
for x in alpha_list:
out_list_inner.append('%s-%d' % (x, y))
out_list.append(out_list_inner)
print out_list
print ['%s-%d' % (x, y) for x in alpha_list if x < 'C' for y in num_list if y < 55]
print ['%s-%d' % (x, y) for x in alpha_list for y in num_list if y < 55 and x < 'C']
print ['%s-%d' % (x, y) for x in alpha_list if y < 55 and x < 'C' for y in num_list]
['%s-%d' % (x, y)
for x in alpha_list
if x < 'C'
for y in num_list
if y < 55]
out_list = []
for x in alpha_list:
if x < 'C':
for y in num_list:
if y < 55:
out_list.append('%s-%d' % (x, y))
print out_list
out_list = []
for x in alpha_list:
for y in num_list:
if y < 55 and x < 'C':
out_list.append('%s-%d' % (x, y))
print out_list
out_list = []
for x in alpha_list:
if y < 55 and x < 'C':
for y in num_list:
out_list.append('%s-%d' % (x, y))
print out_list
vec = [[1,2,3], [4,5,6], [7,8,9]]
print [num for elem in vec for num in elem]
# 3 - Reversed / iterators
rev_old = reversed(alpha_list)
print [i + a for i in alpha_list for a in rev_old]
print [i for i in rev_old]
print rev_old
rev_old = reversed(alpha_list)
out_list = []
for y in alpha_list:
out_list_inner = []
for x in rev_old:
out_list_inner.append('%s-%s' % (x, y))
out_list.append(out_list_inner)
print out_list
rev_list = list(reversed(alpha_list))
print [i + a for i in alpha_list for a in rev_list]
print [i + a for i in alpha_list for a in reversed(alpha_list)]
rev_old = reversed(alpha_list)
print [i + a for i in rev_old for a in alpha_list]
print [i + a for i in alpha_list for a in alpha_list[::-1]]
rev_old = alpha_list[::-1]
print [i + a for i in alpha_list for a in rev_old]
# 4 - Dictionary comprehension
keys = [10,30,50]
print dict((x, y) for x in keys for y in num_list)
print [(x, y) for x in keys for y in num_list]
print [x for x in zip(keys, num_list)]
print dict(zip(keys, num_list))
class Person(object):
def __init__(self, id, name):
self.id = id
self.name = name
def __repr__(self):
return "%s (%d)" % (self.name, self.id)
people = [
Person(1, 'Joe'),
Person(2, 'Marry'),
Person(3, 'Kieth')
]
print dict([(p.id, p) for p in people])
print