Python Filter,map,lambda,reduce, List parsing

Source: Internet
Author: User

Filter usage filter (FUNC,SEQ)

Converts the elements of a SEQ into Func by one generation, using the return value of Func to determine whether to retain or filter

1 def foo (x): 2     return x>334 >>> filter (Foo,range (6))5 [4, 5]

>>> Filter (lambda x:x>3,range (6))
[4, 5]

6 # Note that you only need to write the function name without parameters

Map usage map (FUNC/LAMBDA,SEQ)

By using a function to manipulate each element in the queue, the element is replaced with a return value, note that a new sequence is generated and the original sequence does not change

>>> map (Lambda x:x*2,range (62, 4, 6, 8, ten# the same, here is either a lambda expression, Or is it a function name >>> a[1, 2, 3, 4, 5, 6]>>> Map (foo,a) [False, False, False, True, Tr UE, True]>>> a[1, 2, 3, 4, 5, 6]#foo is to determine if X is greater than three, is true, no returns false
Operations on multiple sequences

>>> map(Lambda X,y:x+y,range (4), Range (5))

Traceback (most recent):
File "<pyshell#84>", line 1, in <module>
Map (Lambda X,y:x+y,range (4), Range (5))
File "<pyshell#84>", line 1, in <lambda>
Map (Lambda X,y:x+y,range (4), Range (5))
typeerror:unsupported operand type (s) for +: ' Nonetype ' and ' int '
>>> map(Lambda X,y:x+y,range (4), Range (4))
[0, 2, 4, 6]

List parsing

[Expression/function, for loop]

>>> [x**2 forXinchRange (6)][0,1, 4, 9, 16, 25]>>> [a**2 forXinchRange (6)][1, 1, 1, 1, 1, 1]>>>#It can be seen that when you write an unrelated variable in a for loop, it simply repeats the result of the expression>>> [foo (x) forXinchRange (6)][false, False, False, False, True, True]#here's the function with parentheses, Foo ibid .>>> [LambdaX:x**2 forXinchRange (6)][<function <Lambda> at 0x01de8870>, <function <Lambda> at 0x01de8830>, <function <Lambda> at 0x01de88f0>, <function <Lambda> at 0x01de8930>, <function <Lambda> at 0x01de8970>, <function <Lambda> at 0x01de89b0>]>>> [(Lambdax:x**2) (x) forXinchRange (6)][0,1, 4, 9, 16, 25]#also you need to think of lambda as a function name, or a return value>>> [x**2 forXinchRange (6)ifX**2>9][16, 25]>>> [(X**2,Y**3) forXinchRange (4) forYinchRange (3)ifX>0 andY>0] [(1, 1), (1, 8), (4, 1), (4, 8), (9, 1), (9, 8)]>>>#consider the subsequent for as nested, and the preceding expression remember parentheses

Reduce iterates over the item order in sequence call function, and if there is starting_value, it can also be called as an initial value

>>> reduce (lambda x,y:x+y,range (1,101))#reduce must accept a two-dollar function, such as above (((1+2) + 3) +4) + ... >>> reduce (lambda x,y:x+y,range (1,101), 5150# ) accepted initial value

Python Filter,map,lambda,reduce, List parsing

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.