Small turtles zero-based learning Python- Homework (21): Function --lambda expression

Test questions:
0. Please use the following function lambda expression into the anonymous function?

fun_A DEF (the X-, the y-= 3):
return the X-* the y-


fun_A the X-the lambda =, = the y-3: the X-* the y-;



1. Please anonymous function below the Cock wire into a common function?

the X-the lambda: the X-IF the else the X-% 2 None

DEF fun_1 (the X-):

IF the X-% 2:
return the X-;
the else:
return None;


2. to feel the changes in your life to bring the program to use anonymous function?
I do not particularly want the function name and brainer


3. You can use the filter () all multiples of 3 and lambda expressions to quickly find it within 100?

>>> list(filter(lambda x : not(x%3), range(1,100)))
[3, 6, 9, 12, 15, 18, 21, 24, 27, 30, 33, 36, 39, 42, 45, 48, 51, 54, 57, 60, 63, 66, 69, 72, 75, 78, 81, 84, 87, 90, 93, 96, 99]

4. Remember list comprehensions do? Can use a list comprehension instead of the filter () and lambda combination, you can do it?

>>> [i for i in range(1, 100) if not(i%3)]
[3, 6, 9, 12, 15, 18, 21, 24, 27, 30, 33, 36, 39, 42, 45, 48, 51, 54, 57, 60, 63, 66, 69, 72, 75, 78, 81, 84, 87, 90, 93, 96, 99]


5. Remember zip it? Using two zip will bind a number of tuples in the form of, for example:
>>> List (zip ([. 1,. 3,. 5,. 7,. 9], [2,. 4,. 6,. 8, 10]))
[ (1, 2), (3, 4), (5, 6), (7, 8), (9, 10)]
, but if I want packaged form flexible list instead of tuples
(desirably [ [1, 2], [3, 4], [5, 6], [7, 8], [9, 10]] this form),
can you do? (Using map and lambda expressions)

List >>> (Map (the lambda X, Y: [X, Y], Range (. 1, 10, 2), Range (2,10,2)))
[[. 1, 2], [. 3,. 4], [5, 6], [7, 8]]


6. the following visual expression of what will print?

make_repeat DEF (n-):
return the lambda S: S * n-

Double = make_repeat (2)
Print (Double (. 8))
Print (Double ( 'FishC'))

>>> = make_repeat Double (2)
>>> Print (Double (. 8))
16
>>> Print (Double ( 'FishC'))
FishCFishC
>>>

Guess you like

Origin www.cnblogs.com/yuxuesang/p/11085381.html