PythonÕýÔò±í´ïʽµÄ³£ÓÃÆ¥ÅäÓ÷¨
ÏÂÃæÁгöPythonÕýÔò±í´ïʽµÄ¼¸ÖÖÆ¥ÅäÓ÷¨£º
1.²âÊÔÕýÔò±í´ïʽÊÇ·ñÆ¥Åä×Ö·û´®µÄÈ«²¿»ò²¿·Ö
regex=ur"" #ÕýÔò±í´ïʽ
if re.search(regex, subject):
do_something()
else:
do_anotherthing()
2.²âÊÔÕýÔò±í´ïʽÊÇ·ñÆ¥ÅäÕû¸ö×Ö·û´®
regex=ur"\Z" #ÕýÔò±í´ïʽĩβÒÔ\Z½áÊø
if re.match(regex, subject):
do_something()
else:
do_anotherthing()
3.´´½¨Ò»¸öÆ¥Åä¶ÔÏó£¬È»ºóͨ¹ý¸Ã¶ÔÏó»ñµÃÆ¥Åäϸ½Ú(Create an object with details about how the regex matches (part of) a string)
regex=ur"" #ÕýÔò±í´ïʽ
match = re.search(regex, subject)
if match:
# match start: match.start()
# match end (exclusive): atch.end()
# matched text: match.group()
do_something()
else:
do_anotherthing()
4.»ñÈ¡ÕýÔò±í´ïʽËùÆ¥ÅäµÄ×Ó´®(Get the part of a string matched by the regex)
regex=ur"" #ÕýÔò±í´ïʽ
match = re.search(regex, subject)
if match:
result = match.group()
else:
result = ""
5. »ñÈ¡²¶»ñ×éËùÆ¥ÅäµÄ×Ó´®(Get the part of a string matched by a capturing group)
regex=ur"" #ÕýÔò±í´ïʽ
match = re.search(regex, subject)
if match:
result = match.group(1)
else:
result = ""
6. »ñÈ¡ÓÐÃû×éËùÆ¥ÅäµÄ×Ó´®(Get the part of a string matched by a named group)
regex=ur"" #ÕýÔò±í´ïʽ
match = re.search(regex, subject)
if match:
result = match.group"groupname")
else:
result = ""
7. ½«×Ö·û´®ÖÐËùÓÐÆ¥ÅäµÄ×Ó´®·ÅÈëÊý×éÖÐ(Get an array of all regex matches in a string)
result = re.findall(regex, subject)
8.±éÀúËùÓÐÆ¥ÅäµÄ×Ó´®(Iterate over all matches in a string)
for match in re.finditer(r"<(.*?)\s*.*?/\1
Ïà¹ØÎĵµ£º
¶Ô Python º¯ÊýµÄ"µ÷ÖÆ"£¬ÊÇÖ¸¶ÔÆä×ö³öºÏºõÐèÇóµÄÉèÖ᣾ßÌåµÄµ÷ÖÆ·½·¨£¬Êǽ«Æä²ÎÊýÉèΪ¹Ì¶¨Öµ£¨³£Êý£©¡£
É趨µ¥Ò»µÄ²ÎÊýÖµ
ÔÏȵĺ¯ÊýÊÇÕâÑùµÄ£º
>>> def foo(cooked, standard):
... print "foo called with cooked: %s, standard: %s" % \
... (cooked, standard)
µ÷ÓÃËü£º
>>> foo('a', ......
´´½¨Áбí
sample_list = ['a',1,('a','b')]
Python Áбí²Ù×÷
sample_list = ['a','b',0,1,3]
µÃµ½ÁбíÖеÄijһ¸öÖµ
value_start = sample_list[0]
end_value =
sample_list[-1]
ɾ³ýÁбíµÄµÚÒ»¸öÖµ
del sample_list[0]
ÔÚÁбíÖвåÈëÒ»¸öÖµ
sample_list[0:0] = ['sample value']
µÃµ½ÁбíµÄ³¤¶È
list_length = ......
Python±ê×¼¿â-randomÄ£¿é
random Ä£¿é°üº¬Ðí¶àËæ»úÊýÉú³ÉÆ÷. »ù±¾Ëæ»úÊýÉú³ÉÆ÷(»ùÓÚ Wichmann ºÍ Hill , 1982 µÄÊýѧÔËËãÀíÂÛ) ¿ÉÒÔͨ¹ýºÜ¶à·½·¨·ÃÎÊ, Èç Example 2-29 Ëùʾ. 2.17.0.1. Example 2-29. ʹÓà random Ä£¿é»ñµÃËæ»úÊý×Ö File: random-example-1.py import random for i i
¡¡¡¡
random Ä£¿é°üº¬Ðí¶àËæ»úÊýÉ ......
DecoratorsÊÇpythonÖбȽÏÄÑÒÔÀí½âµÄ¶«Î÷£¬µ±È»Èç¹ûÄã½Ó´¥¹ýjavaµÄannotation£¬»á·¢ÏÖÕâ¸öDecoratorsÔÚÓï·¨ÉϷdz£µÄÏàËÆ£¬µ«ÊÇÁ½ÕßµÄÉè¼Æ¶¯»úȴûʲô¹²Í¬µã£»
ÕâÀï½²µÄpythonÖеÄdecoratorsÊǶÔpythonÖеÄfunction/method×ö×°ÊΣ¬ÕâЩÐÞÊνöÊǵ±ÉùÃ÷Ò»¸öº¯Êý»òÕß·½·¨µÄʱºò£¬²Å»áÓ¦ÓõĶîÍâµ÷Óá£
pythonÖеÄdecorator·Ö ......