PythonµÄlambdaº¯ÊýÓëÅÅÐò
ǰ¼¸Ìì¿´µ½ÁËÒ»ÐÐÇó1000µÄ½×³ËµÄPython´úÂ룺
print
reduce
(
lambda
x
,
y
:
x
*
y
,
range
(
1
,
1001
))
Ò»ÏÂ×Ó±»python´úÂëµÄ¾«¼ò
Óë½ô´ÕËùÕÛ·þ£¬¹Ê¶Ô´úÂë½øÐÐÁ˼òµ¥µÄ·ÖÎö¡£
reduceÓërange¶¼ÊÇPythonµÄÄÚÖú¯Êý¡£
range£¨1£¬1001£©
±íʾÉú³É1µ½1000µÄÁ¬ÐøÕûÊýÁÐ±í£¨List£©¡£
reduce£¨functionA£¬iterableB£©£¬functionAΪÐèÒªÁ½
¸ö±äÁ¿µÄº¯Êý£¬²¢·µ»ØÒ»¸öÖµ¡£iterableBΪ¿Éµü´ú±äÁ¿£¬ÈçListµÈ¡£reduceº¯Êý½«BÖеÄÔªËØ´Ó×óµ½ÓÒÒÀ´Î´«È뺯ÊýAÖУ¬ÔÙÓú¯ÊýA·µ»ØµÄ
½á¹ûÌæ´ú´«ÈëµÄ²ÎÊý£¬·´¸´Ö´ÐУ¬Ôò¿É½«B
reduce³ÉÒ»¸öµ¥Öµ¡£ÔÚ´Ë£¬Êǽ«1µ½1000µÄÁ¬ÐøÕûÊýÁÐ±í´«Èëlambdaº¯Êý²¢ÓÃÁ½¸öÊýµÄ»ýÌæ»»ÁбíÖеÄÊý£¬Êµ¼ÊµÄ¼ÆËã¹ý³ÌΪ£º(...
((1×2)×3)×4)×...×1000)£¬×îºóµÄ½á¹û¼´1000µÄ½×³Ë¡£
ÏÂÃæÀ´½éÉÜÒ»ÏÂlambdaº¯Êý¡£
lambda
º¯ÊýÊÇÒ»ÖÖ¿ìËÙ¶¨Òåµ¥ÐеÄ×îСº¯Êý£¬ÊÇ´Ó Lisp
½èÓÃÀ´µÄ£¬¿ÉÒÔÓÃÔÚÈκÎÐèÒªº¯ÊýµÄµØ·½¡£ÏÂÃæµÄÀý×ӱȽÏÁË´«Í³µÄº¯ÊýÓëlambdaº¯ÊýµÄ¶¨Ò巽ʽ£º
1
>>>
def
f
(
x
,y):
2
...
return
x
*
y
3
...
4
>>>
f
(
2,3
)
5
6
6
>>>
g
=
lambda
x
,y:
x
*
y
7
>>>
g
(
2,3
)
8
6
¿ÉÒÔ¿´µ½£¬Á½¸öº¯Êý
µÃµ½µÄ½á¹ûÒ»Ñù£¬¶ø¶ÔÓÚʵÏÖ¼òµ¥¹¦Äܵĺ¯ÊýÀ´Ëµ£¬Ê¹ÓÃlambdaº¯ÊýÀ´¶¨Òå¸ü¼Ó¾«¼òÁé»î£¬»¹¿ÉÒÔÖ±½Ó°Ñº¯Êý¸³Öµ¸øÒ»¸ö±äÁ¿£¬ÓñäÁ¿ÃûÀ´±íʾº¯ÊýÃû¡£
Æäʵlambdaº¯ÊýÔںܶàʱºò¶¼ÊDz»ÐèÒª¸³Öµ¸øÒ»¸ö±äÁ¿µÄ£¨ÈçǰÎÄÖÐÇó½×³ËµÄ¹ý³Ì£©¡£
ʹÓÃlambdaº¯Êý»¹ÓÐһЩעÒâÊÂÏ
lambda º¯Êý¿ÉÒÔ½ÓÊÕÈÎÒâ¶à¸ö²ÎÊý (°üÀ¨¿ÉÑ¡²ÎÊý) ²¢ÇÒ·µ»Øµ¥¸ö±í´ïʽµÄÖµ¡£
lambda º¯Êý²»Äܰüº¬ÃüÁ°üº¬µÄ±í´ïʽ²»Äܳ¬¹ýÒ»¸ö¡£
ÏÂÃæ¼òµ¥ÑÝʾһÏÂÈçºÎʹÓÃlambdaº¯ÊýʵÏÖ×Ô¶¨ÒåÅÅÐò¡£
01
class
People
:
02
age
=
0
03
gender
=
'male'
04
05
def
__init__
(
self
,
age
,
gender
):
06
self
.
age
=
age
07
&
Ïà¹ØÎĵµ£º
ÕýÔò±í´ïʽÊÇËÑË÷¡¢Ìæ»»ºÍ½âÎö¸´ÔÓ×Ö·ûģʽµÄÒ»ÖÖÇ¿´ó¶ø±ê×¼µÄ·½·¨.
ÕýÔò±í´ïʽ£¨»ò RE£©ÊÇÒ»ÖÖСÐ͵ġ¢¸ß¶Èרҵ»¯µÄ±à³ÌÓïÑÔ£¬£¨ÔÚPythonÖУ©ËüÄÚǶÔÚPythonÖУ¬²¢Í¨¹ý re Ä£¿éʵÏÖ.×Ö·û´®Ò²Óкܶ෽·¨,¿ÉÒÔ½øÐÐËÑË÷ (index¡¢find ºÍ count)¡¢Ìæ»» (replace) ºÍ½â
Îö (split),µ«ËüÃǽöÏÞÓÚ´¦Àí×î¼òµ¥µÄÇé¿ö
re Ä£¿éʹ P ......
Python Áбí
listÊÇ´¦ÀíÒ»×éÓÐÐòÏîÄ¿µÄÊý¾Ý½á¹¹£¬¼´Äã¿ÉÒÔÔÚÒ»¸öÁбíÖд洢һ¸ö ÐòÁÐ µÄÏîÄ¿¡£¼ÙÏëÄãÓÐÒ»¸ö¹ºÎïÁÐ±í£¬ÉÏÃæ¼ÇÔØ×ÅÄãÒªÂòµÄ¶«Î÷£¬Äã¾ÍÈÝÒ×Àí½âÁбíÁË¡£Ö»²»¹ýÔÚÄãµÄ¹ºÎï±íÉÏ£¬¿ÉÄÜÿÑù¶«Î÷¶¼¶À×ÔÕ¼ÓÐÒ»ÐУ¬¶øÔÚPythonÖУ¬ÄãÔÚÿ¸öÏîĿ֮¼äÓöººÅ·Ö¸î¡£
ÁбíÖеÄÏîĿӦ¸Ã°üÀ¨ÔÚ·½À¨ºÅÖУ¬ÕâÑùPython¾ÍÖªµÀÄãÊ ......
Python ×Öµä
×ÖµäÀàËÆÓÚÄãͨ¹ýÁªÏµÈËÃû×Ö²éÕÒµØÖ·ºÍÁªÏµÈËÏêϸÇé¿öµÄµØÖ·²¾£¬¼´£¬ÎÒÃǰѼü£¨Ãû×Ö£©ºÍÖµ£¨ÏêϸÇé¿ö£©ÁªÏµÔÚÒ»Æð¡£×¢Ò⣬¼ü±ØÐëÊÇΨһµÄ£¬¾ÍÏñÈç¹ûÓÐÁ½¸öÈËÇ¡ÇÉͬÃûµÄ»°£¬ÄãÎÞ·¨ÕÒµ½ÕýÈ·µÄÐÅÏ¢¡£
×¢Ò⣬ÄãÖ»ÄÜʹÓò»¿É±äµÄ¶ÔÏ󣨱ÈÈç×Ö·û´®£©À´×÷Ϊ×ÖµäµÄ¼ü£¬µ«ÊÇÄã¿ÉÒÔ²»¿É±ä»ò¿É±äµÄ¶ÔÏó×÷Ϊ×Öµäµ ......
Python ÐòÁÐ
ÁÐ±í¡¢Ôª×éºÍ×Ö·û´®¶¼ÊÇÐòÁУ¬µ«ÊÇÐòÁÐÊÇʲô£¬ËüÃÇΪʲôÈç´ËÌØ±ðÄØ£¿ÐòÁеÄÁ½¸öÖ÷ÒªÌØµãÊÇË÷Òý²Ù×÷·ûºÍÇÐÆ¬²Ù×÷·û¡£Ë÷Òý²Ù×÷·ûÈÃÎÒÃÇ¿ÉÒÔ´ÓÐòÁÐÖÐץȡһ¸öÌØ¶¨ÏîÄ¿¡£ÇÐÆ¬²Ù×÷·ûÈÃÎÒÃÇÄܹ»»ñÈ¡ÐòÁеÄÒ»¸öÇÐÆ¬£¬¼´Ò»²¿·ÖÐòÁС£
ʹÓÃÐòÁÐ
Àý9.5 ʹÓÃÐòÁÐ
#!/usr/bin/python
# Filename: seq.py
shoplist ......
µÚÒ»²½£º´î½¨ÊÖ»úÔËÐÐÆ½Ì¨
ÒªÔÚS60ÊÖ»úÉÏÔËÐÐPY¿ª·¢µÄÈí¼þ,Ê×ÏȾÍÒª´î½¨ÊÖ»úPYÔËÐл·¾³.ŵ»ùÑÇÊÖ»ú±¾Éí²»Ö§³ÖPython,ËùÒÔÒª°²×°Python²å¼þ.
PS£ºÒ»¶¨Òª°²×°ÔÚCÅÌ£¡ÎÒÒ»¿ªÊ¼×°E£¬½á¹û³öÏÖÁ˺ÜÑÏÖØµÄÎÊÌ⣡²»¹ý,×î½üÍøÉÏ·¢ÏÖÓÐEÅ̰æµÄÀ²,Äã¿ÉÒÔ×Ô¼ºÊÔÑéһϣ¬¹þ¹þ
PythonScriptShell_1_4_5_3rdEd.SIS
Pyth ......