author | Oleksandr Gavenko <gavenkoa@gmail.com> |
Sat, 10 Feb 2018 01:36:16 +0200 | |
changeset 2229 | 1a0b6597e594 |
parent 1929 | 3ce74b5f43c9 |
permissions | -rw-r--r-- |
1929
3ce74b5f43c9
List comprehension vs generator expression.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff
changeset
|
1 |
|
3ce74b5f43c9
List comprehension vs generator expression.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff
changeset
|
2 |
=============== |
3ce74b5f43c9
List comprehension vs generator expression.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff
changeset
|
3 |
Python syntax |
3ce74b5f43c9
List comprehension vs generator expression.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff
changeset
|
4 |
=============== |
3ce74b5f43c9
List comprehension vs generator expression.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff
changeset
|
5 |
.. contents:: |
3ce74b5f43c9
List comprehension vs generator expression.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff
changeset
|
6 |
:local: |
3ce74b5f43c9
List comprehension vs generator expression.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff
changeset
|
7 |
|
3ce74b5f43c9
List comprehension vs generator expression.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff
changeset
|
8 |
List comprehension |
3ce74b5f43c9
List comprehension vs generator expression.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff
changeset
|
9 |
================== |
3ce74b5f43c9
List comprehension vs generator expression.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff
changeset
|
10 |
|
3ce74b5f43c9
List comprehension vs generator expression.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff
changeset
|
11 |
List comprehension produce complete list with compact syntax:: |
3ce74b5f43c9
List comprehension vs generator expression.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff
changeset
|
12 |
|
3ce74b5f43c9
List comprehension vs generator expression.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff
changeset
|
13 |
[x**2 for x in xrange(1,10)] |
3ce74b5f43c9
List comprehension vs generator expression.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff
changeset
|
14 |
[x+y for x in xrange(1,10) for y in xrange(1,10)] |
3ce74b5f43c9
List comprehension vs generator expression.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff
changeset
|
15 |
[x-y for x in xrange(1,10) for y in xrange(1,10) if x>y] |
3ce74b5f43c9
List comprehension vs generator expression.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff
changeset
|
16 |
|
3ce74b5f43c9
List comprehension vs generator expression.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff
changeset
|
17 |
xlist = [x**2 for x in xrange(1,10)] |
3ce74b5f43c9
List comprehension vs generator expression.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff
changeset
|
18 |
print xlist[0], xlist[2], xlist[-1] |
3ce74b5f43c9
List comprehension vs generator expression.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff
changeset
|
19 |
print xlist[2:4] |
3ce74b5f43c9
List comprehension vs generator expression.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff
changeset
|
20 |
print sum(xlist), sum(xlist) |
3ce74b5f43c9
List comprehension vs generator expression.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff
changeset
|
21 |
|
3ce74b5f43c9
List comprehension vs generator expression.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff
changeset
|
22 |
Generator expression |
3ce74b5f43c9
List comprehension vs generator expression.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff
changeset
|
23 |
==================== |
3ce74b5f43c9
List comprehension vs generator expression.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff
changeset
|
24 |
|
3ce74b5f43c9
List comprehension vs generator expression.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff
changeset
|
25 |
Generator expression produce generator with compact syntax:: |
3ce74b5f43c9
List comprehension vs generator expression.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff
changeset
|
26 |
|
3ce74b5f43c9
List comprehension vs generator expression.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff
changeset
|
27 |
(x**2 for x in xrange(1,10)) |
3ce74b5f43c9
List comprehension vs generator expression.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff
changeset
|
28 |
(x+y for x in xrange(1,10) for y in xrange(1,10)) |
3ce74b5f43c9
List comprehension vs generator expression.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff
changeset
|
29 |
|
3ce74b5f43c9
List comprehension vs generator expression.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff
changeset
|
30 |
print [x for x in (x**2 for x in xrange(1,10))] |
3ce74b5f43c9
List comprehension vs generator expression.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff
changeset
|
31 |
|
3ce74b5f43c9
List comprehension vs generator expression.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff
changeset
|
32 |
xgen = (x**2 for x in xrange(1,10)) |
3ce74b5f43c9
List comprehension vs generator expression.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff
changeset
|
33 |
try: |
3ce74b5f43c9
List comprehension vs generator expression.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff
changeset
|
34 |
xgen[0] |
3ce74b5f43c9
List comprehension vs generator expression.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff
changeset
|
35 |
except TypeError: |
3ce74b5f43c9
List comprehension vs generator expression.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff
changeset
|
36 |
print "'generator' object has no attribute '__getitem__'" |
3ce74b5f43c9
List comprehension vs generator expression.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff
changeset
|
37 |
print sum(xlist), sum(xlist) |
3ce74b5f43c9
List comprehension vs generator expression.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff
changeset
|
38 |
|
3ce74b5f43c9
List comprehension vs generator expression.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff
changeset
|
39 |
Parentheses is required only if expression is ambiguous. |
3ce74b5f43c9
List comprehension vs generator expression.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff
changeset
|
40 |