Python knowledge points, small usage

Source: Internet
Author: User



1. Anonymous function lamdba x:x**2def f (x):     return x**2g = lambda x:x**2     print map (F,range ())     print map (g,range) 2.map function     print map (F,range (Ten))     print map (G,range (10)) 3. Sort function sorted    dic1 = {        9:2,         4:6,        8:1,          ' a ': 10    }    print dic1     print  sorted (Dic1.items ())     print  sorted ( Dic1.items (), key=lambda x:x[0])     print  sorted (Dic1.items (), Key=lambda &NBSP;X:X[1]) 4. Iterator Http://www.liaoxuefeng.com/wiki/0014316089557264a6b348958f449949df42a6d3a2e542c000/ 00143178254193589df9c612d2449618ea460e7a672a366000 iterators only record a start, and an end, and the current position, each cycle one step down the benefits: Save memory empty Where: Xrange (), Xreadlines () import timedef  Run ():    print  ' test 1 '     time.sleep (1)      yield 1   # #这个相当于reture  1  But yield 1  words, when you execute X.next again () You can also come back from this place and start doing the following until you encounter the next yield and then exit                  #  Reture After exiting is unable to return to perform the following content     print  ' test 2 '      time.sleep (1)     yield 2    print  ' test  3 '     time.sleep (1)     yield 3    print   ' Test 4 '     time.sleep (1)     yield 4     print  ' test 5 ' Task = run () task.next () print  "--------do sth 1 --- --------"Task.next () Print&nbsP; " --------do sth 2 -----------"Task.next () print "--------do sth 3 ----------- "Task.next () print "--------do sth 4 -----------"Task.next () print "--------Do sth  5 -----------"Task.next ()     # #这个时候会出现StopIteration报错 # #好处, You can have it and the main program switch back and forth to invoke other content 5. Regular Expression >>> import re>>> a =  ' asdasd3as9#23_454/45 (*_dfdf_\\~r) 3 ' >>> m = re.search (' DFD ', a) >>> print  m<_sre. Sre_match object at 0x10221a030>>>> print m.group () dfd>>>> >>>>> m = re.findall (' [a-za-z]+ ', a)   # #查找所有字母, tuple format returned >>>  print m[' asdasd ',  ' as ',  ' dfdf ',  ' R ']>>>>>>>>> m  = re.split (' \d+ ', a) >>> print m[' asdasd ',  ' as ',  ' # ',  ' _ ',  '/' ,  ' (*_dfdf_\\~r) ',  ']>>&Gt;>>>>>> m = re.sub (' \d+ ', "|", a,count=3) &GT;&GT;&GT;&NBSP;PRINT&NBSP;MASDASD |AS|#|_454/45 (*_dfdf_\~r) 3>>>>>> a ' Asdasd3as9#23_454/45 (*_dfdf_\\~r) 3 ' >>>> >> a= ' 192.173.12.121 '    # #匹配ip >>> a ' 192.173.12.121 ' >>>  Re.search (' (\d+) (\.\d+) {3} ', a). Group () ' 192.173.12.121 ' 6. Recursive function Def calc (n):     print   '-------------> ',n    if n > 0:         calc ((N/2)) Calc (-------------> 20-------------> 10------------->  5-------------> 2-------------> 1-------------> 0


This article from the "Struggle Bar" blog, reproduced please contact the author!

Python knowledge points, small usage

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.