Python boring summary, Python boring Summary

Source: Internet
Author: User

Python boring summary, Python boring Summary
 

When I was bored at the company, I read the python code written by my predecessors and suddenly found a more interesting python expression:

1 lambda x, y: x + y

At first glance, this should be a similar method. I checked it online, so I would like to summarize it.

Lambda: Code on

Representative parameters of the front side of the colon
Expression behind the colon
The return value type is related to the parameter.

1 >>> lambda x: x 2 <function <lambda> at 0x01B50730> # if this is not done, 3 >>> 4 >>> 5 >>> la = lambda x: x + y 6 >>> la (2) 7 Traceback (most recent call last): 8 File "<stdin>", line 1, in <module> 9 File "<stdin>", line 1, in <lambda> 10 NameError: global name 'y' is not defined11 >>>12 >>>>13 >>> la = lambda x, y: x + y14 >>>> 15 >>>> 16 >>> la (1, 2) 17 3

 

** Return value:

1 >>> la (1, range (5) # List 2 [0, 1, 2, 3, 4] 3 >>> 4 >>> 5 >>> la (1, (1, 2) # tuples 6 (1, 2) 7 >>> la (1, 'xyz') # string 8 'xyz'

 

** From here we can see that lambda is quite convenient. If it is suitable for complex expressions, do not use lambda for personal opinions

Filter:

First parameter: Method
Second parameter: Sequence
Generates a list of each element in the sequence by iterative means.

1 >>> def fil(x):2 ...     return x % 2 != 0 and x % 3 != 03 ...4 >>>5 >>>6 >>> filter(fil,range(1,100))7 [1, 5, 7, 11, 13, 17, 19, 23, 25, 29, 31, 35, 37, 41, 43, 47, 49, 53, 55, 59, 618 , 65, 67, 71, 73, 77, 79, 83, 85, 89, 91, 95, 97]9 >>>

 

** If the first parameter is set to None, only the values in the sequence are traversed, for example:

1 >>> filter(None,range(1,100))2 [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22,3 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42,4 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62,5 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82,6 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99]7 >>>

 

Reduce

First parameter: Method Name
Second parameter: Sequence
The third parameter is standby. If it is not passed, it is 0.
I really don't understand why I got this name. Please ignore my complaints and check out the code.
The returned value is related to the parameter.

1 >>> def add (x, y): 2... print 'X = '+ str (x), 'y =' + str (y) 3... return x + y 4... 5 >>> reduce (add, range (10) 6 x = 0 y = 1 # When the 3rd parameters are not passed, we can see that the value of x is a given value, then the value is 7 x = 1 y = 2 8 x = 3 y = 3 9 x = 6 y = 410 x = 10 y = 511 x = 15 y = 612 x = 21 y = 713 x = 28 y = 814 x = 36 y = 915 45

 

** When the third parameter is included

1 >>> reduce (add, range (10), 20) 2 x = 20 y = 0 # From here we can see that, reduce is the value given from the third parameter and then iterated 3 x = 20 y = 1 4 x = 21 y = 2 5 x = 23 y = 3 6 x = 26 y = 4 7 x = 30 y = 5 8 x = 35 y = 6 9 x = 41 y = 710 x = 48 y = 811 x = 56 y = 912 6513> reduce (add, 'xyz') 14 x = x y = y15 x = xy y = z16 'xyz' 17 >>> reduce (add, 'xyz', 10) 18 x = 10 y = x19 Traceback (most recent call last): 20 File "<stdin>", line 1, in <module> 21 File "<stdin> ", line 3, in add22 TypeError: unsupported operand type (s) for +: 'int' and 'str' 23 >>>> 24 >>> 25 >>> reduce (add, 'xyz', '10') 26 x = 10 y = x27 x = 10x y = y28 x = 10xy y = z29 '10xy' 30 >>>

 

Map

First parameter: Method Name
The second parameter: sequence, or multiple sequences (provided that the method must support multiple parameters)
The returned value is a list.

1 >>> map (add, range (10) # Here we can see that the method of reading parameters is different from that of reduce. Actually, it's similar, because the third reduce parameter has initialization 2 Traceback (most recent call last): 3 File "<stdin>", line 1, in <module> 4 TypeError: add () takes exactly 2 arguments (1 given) 5 >>>> 6 >>> 7 >>> map (add, range (10), range (10 )) 8 x = 0 y = 0 9 x = 1 y = 110 x = 2 y = 211 x = 3 y = 312 x = 4 y = 413 x = 5 y = 514 x = 6 y = 615 x = 7 y = 716 x = 8 y = 817 x = 9 y = 918 [0, 2, 4, 6, 8, 10, 12, 14, 16, 18] 19 >>> 20 >>> map (ret, 'x ') 21 x = x22 ['X'] 23> 24> 25> map (ret, (127) 26 x = 228 x = [1, 2] 29 >>> 30 >>> 31 >>> map (ret, [133]) 32 x = 234 x = [1, 2] 35 >>>

 

If the method in map has only one parameter, only one sequence can be passed in. If there are multiple, multiple sequences are passed in. It's strange to iterate the values in each sequence, and I don't know what it is useful in the coding process.

In general, I seldom use these methods in the python coding process. It is estimated that these methods will be used during the interview.

Generated by haroopad

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.