Академический Документы
Профессиональный Документы
Культура Документы
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
:
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 - ,
. .
2.
. , list comprehensions
. ,
, .. (
..), .
:
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.