Вы находитесь на странице: 1из 6

#4.

Python: .
1.
. map filter
( lambda).
.
>>> f1 = lambda x,y: x+y # ,
>>> f1(2,3)
5
>>> f2 = lambda x: x*2 if x>5 else None
>>> f2(4) # , None
>>> f2(6)
12
lambda-, , :
lambda [parameters]: expression
parameters , ..
>>> var = 10
>>> f1 = lambda: var*2
, . expression
, lambda-. ..,
:
>>> f1 = lambda x: x*2 if x>5 else None
:
>>> f1 = lambda x: lambda y: x+y
>>>f2 = f1(5) # f2 - , + 5
>>>f2(10)
15
:

>>> f1 = lambda x: for i in range(x): print i


, , ?
>>> f1 = lambda x: x*2 and x if x>5 else None
>>> f1(6)
?
(
http://docs.python.org/2/reference/expressions.html#and)1

map
:
>>> map(lambda x: x*2, [1,2,3])
[2,4,6]
>>> map(lambda x,y: x*y, [1,2,3],(4,5,6))
[4,10,18]
>>> map(None, [1,2,3], [4,5,6])
[(1,4),(2,5),(3,6)]
: map(function, iterable, ...). map list.
filter :
>>> filter(lambda x: x>2, [1,2,3])
[3]
>>> filter(None, [0,1,0,2,3])
[1,2,3]
: filter(function, iterable). iterable - ,
. .

: x and y Python x. x True,


y - ,
. 12 6. 12
True , - 6.
.
1

2.
. , list comprehensions
. ,

b_list = [somefunc(elem) for elem in a_list]


,
b_list = []
for elem in a_list:
b_list.append(somefunc(elem))
. , ,
.
. :
def list_loop(*args, **kwargs):
result = []
for i in args[0]:
result.append(2*i)
return result
def list_comprehension(*args, **kwargs):
return [2*x for x in args[0]]
,
2, for, - list comprehension.

time clock()(. , ,
Win Unix):
import time
somelist = range(1000)
stime = time.clock()
res = list_loop(somelist)
etime = time.clock() - stime
print Elapsed %f seconds % etime
, , :

, .. (
..), .
:
total_time = 0.
for i in range(runs):
stime = time.clock()
res = list_loop(somelist)
total_time += time.clock() - stime
print Elapsed %f seconds in average % total_time/runs
,
(. timing(runs)
decorator.py).
:
( ),
(_timing __timing).
.
) : for, list comprehension
map.
student@student:~/some/path> python decorator.py
, ,
(, for, list
comprehension filter).
b) PyPy - Python Python, JIT (Just In
Time compilation) - , ), . PyPy
CPython, ,
.
PyPy. PyPy openSUSE
Python:

student@student:~/some/path> su
Password: <enter root pwd here>
student:/any/path # zypper ar
http://download.opensuse.org/repositories/devel:/languages:/python:/Factory/
openSUSE_12.3/ Python-Factory
( , ).
PyPy
student:/any/path # zypper install pypy
Loading repository data...
Reading installed packages...
Resolving package dependencies...
The following NEW packages are going to be installed:
pypy pypy-libs
2 new packages to install.
Overall download size: 14.7 MiB. After the operation, additional 110.4 MiB
will be used.
Continue? [y/n/?] (y): y
, ,
Linux.
:)
c) a) b)? :
? ,
( 24 25
decorator.py) - .
. Github:
student@student:~> git clone https://github.com/PhtRaveller/kharkiv-hpc.git
, (.. ,
2) :
student@student:~/some/path> git pull -u origin

.
1. , Python, 3- , -, 2009.
2. :
http://docs.python.org/2/library/functions.html
3. Python:
http://docs.python.org/2/tutorial/datastructures.html#functional-programming-to
ols
4. list comprehensions:
http://docs.python.org/2/tutorial/datastructures.html#list-comprehensions
5. Marty Alchin, Pro Python, Apress, 2010 - 3 Functions,
Decorators.

Вам также может понравиться