In this blog, I am going to talk about some
advanced technology in Python. Why they are called acvanced is because they are
not very common in other programming languages.
comprehension:
comprehension is an efficient way to build
up tuple, list or dict.If we want to get the even number between 1
and 10, we may get by a traditional programming way.
evens = []
for index in xrange(2,10,2):
evens.append(index)
here is the list comprehension:
evens = [ i for in range(10) if i %2 == 2]
you may use comprehension to build a
dictionary like this:
mydict = { i : "number %d" %i for
i in xrange(10)}
the advantage of using comprehension:
- for human readable
- more compact and faster than an explicit
for
loop building a list/dict. Will generate all element first then assign to list/tuple/dict
iterator:
iterator is a way to visit the (set,
string, list, tuple) from the first element to the last element
it is the only way to traverse
the set.
There are two ways to build an
iterator
Using iter(iterable) or
iterable.__iter__() to generate an iterator.
Iterator has a lazy evaluation
feature, so it can be very efficient when traverse an big or even infinative set
any class implemented
__iter__() function, it can be used as iteralbe for iterator
The iterator can be traversed
by next function, and it will raise
StopIteration exception
The common code is like
try:
while True:
element = myiter.next()
#do something here
except StopIteration:
pass
generator:
Generators are a simple and powerful funciton
for creating iterators. They are written like regular functions but use the
yield statement whenever they want to return data. Each time next() is called,
the generator resumes where it left-off (it remembers all the data values and
which statement was last executed).
For example, the fibonacci funciton can be
a generator
def fibonacci():
a,b
= 0, 1
while
True:
yield
b
a
, b = b , a+b
fib = fibonacci()
fib.next()
fib.next()
fib.next()
when we need a function whose return value
will be used in a list or in a loop, generator should be considerd.
No comments:
Post a Comment