mirror of
https://github.com/adambard/learnxinyminutes-docs.git
synced 2024-11-26 09:21:00 +03:00
[python/en] Set, dict, and generator comprehensions (#2298)
* Add set and dict comprehensions for python 2 and 3 * Clean up formatting and generator explanation * Include documentation for generator comprehensions
This commit is contained in:
parent
5dee671bfe
commit
e053806775
@ -549,6 +549,10 @@ filter(lambda x: x > 5, [3, 4, 5, 6, 7]) # => [6, 7]
|
||||
[add_10(i) for i in [1, 2, 3]] # => [11, 12, 13]
|
||||
[x for x in [3, 4, 5, 6, 7] if x > 5] # => [6, 7]
|
||||
|
||||
# You can construct set and dict comprehensions as well.
|
||||
{x for x in 'abcddeef' if x in 'abc'} # => {'d', 'e', 'f'}
|
||||
{x: x**2 for x in range(5)} # => {0: 0, 1: 1, 2: 4, 3: 9, 4: 16}
|
||||
|
||||
|
||||
####################################################
|
||||
## 5. Classes
|
||||
@ -704,7 +708,7 @@ def double_numbers_generator(iterable):
|
||||
|
||||
# Running the same code as before, but with a generator, now allows us to iterate
|
||||
# over the values and doubling them one by one as they are being consumed by
|
||||
# our logic. Hence as soon as we see a value > 5, we stop break out of the
|
||||
# our logic. Hence as soon as we see a value > 5, we break out of the
|
||||
# loop and don't need to double most of the values sent in (MUCH FASTER!)
|
||||
for value in double_numbers_generator(xrange(1000000)): # `test_generator`
|
||||
print value
|
||||
@ -717,6 +721,16 @@ for value in double_numbers_generator(xrange(1000000)): # `test_generator`
|
||||
# `range` would return back and array with 1000000 values for us to use
|
||||
# `xrange` would generate 1000000 values for us as we request / iterate over those items
|
||||
|
||||
# Just as you can create a list comprehension, you can create generator
|
||||
# comprehensions as well.
|
||||
values = (-x for x in [1,2,3,4,5])
|
||||
for x in values:
|
||||
print(x) # prints -1 -2 -3 -4 -5 to console/terminal
|
||||
|
||||
# You can also cast a generator comprehension directly to a list.
|
||||
values = (-x for x in [1,2,3,4,5])
|
||||
gen_to_list = list(values)
|
||||
print(gen_to_list) # => [-1, -2, -3, -4, -5]
|
||||
|
||||
|
||||
# Decorators
|
||||
|
@ -601,6 +601,10 @@ list(filter(lambda x: x > 5, [3, 4, 5, 6, 7])) # => [6, 7]
|
||||
[add_10(i) for i in [1, 2, 3]] # => [11, 12, 13]
|
||||
[x for x in [3, 4, 5, 6, 7] if x > 5] # => [6, 7]
|
||||
|
||||
# You can construct set and dict comprehensions as well.
|
||||
{x for x in 'abcddeef' if x in 'abc'} # => {'d', 'e', 'f'}
|
||||
{x: x**2 for x in range(5)} # => {0: 0, 1: 1, 2: 4, 3: 9, 4: 16}
|
||||
|
||||
|
||||
####################################################
|
||||
## 5. Modules
|
||||
@ -816,7 +820,7 @@ if __name__ == '__main__':
|
||||
sup.age = 100
|
||||
print(sup.age)
|
||||
|
||||
# Inherited attribute from 2nd ancestor whose default value was overriden
|
||||
# Inherited attribute from 2nd ancestor whose default value was overridden.
|
||||
print('Can I fly? ' + str(sup.fly))
|
||||
|
||||
|
||||
@ -825,29 +829,35 @@ if __name__ == '__main__':
|
||||
## 7. Advanced
|
||||
####################################################
|
||||
|
||||
# Generators help you make lazy code
|
||||
# Generators help you make lazy code.
|
||||
def double_numbers(iterable):
|
||||
for i in iterable:
|
||||
yield i + i
|
||||
|
||||
# A generator creates values on the fly.
|
||||
# Instead of generating and returning all values at once it creates one in each
|
||||
# iteration. This means values bigger than 15 wont be processed in
|
||||
# double_numbers.
|
||||
# We use a trailing underscore in variable names when we want to use a name that
|
||||
# would normally collide with a python keyword
|
||||
range_ = range(1, 900000000)
|
||||
# will double all numbers until a result >=30 found
|
||||
for i in double_numbers(range_):
|
||||
# Generators are memory-efficient because they only load the data needed to
|
||||
# process the next value in the iterable. This allows them to perform
|
||||
# operations on otherwise prohibitively large value ranges.
|
||||
# NOTE: `range` replaces `xrange` in Python 3.
|
||||
for i in double_numbers(range(1, 900000000)): # `range` is a generator.
|
||||
print(i)
|
||||
if i >= 30:
|
||||
break
|
||||
|
||||
# Just as you can create a list comprehension, you can create generator
|
||||
# comprehensions as well.
|
||||
values = (-x for x in [1,2,3,4,5])
|
||||
for x in values:
|
||||
print(x) # prints -1 -2 -3 -4 -5 to console/terminal
|
||||
|
||||
# You can also cast a generator comprehension directly to a list.
|
||||
values = (-x for x in [1,2,3,4,5])
|
||||
gen_to_list = list(values)
|
||||
print(gen_to_list) # => [-1, -2, -3, -4, -5]
|
||||
|
||||
|
||||
# Decorators
|
||||
# in this example beg wraps say
|
||||
# Beg will call say. If say_please is True then it will change the returned
|
||||
# message
|
||||
# In this example `beg` wraps `say`. If say_please is True then it
|
||||
# will change the returned message.
|
||||
from functools import wraps
|
||||
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user