Python匿名函数
1. 请利用filter()过滤出1~100中平方根是整数的数,即结果应该是: [1, 4, 9, 16, 25, 36, 49, 64, 81, 100] 1 import math 2 def func(x): 3 return math.sqrt(x) % 1 == 0 4 ret = filter(func,range(0,101)) 5 for i in ret: 6 print(i) 2. 列表按照其中每一个值的绝对值排序 li = [1,-2,3,-48,78,9] print(sorted(li,key = abs)) 结果: [1, -2, 3, 9, -48, 78] 3. 列表按照每一个元素的len排序 li = [(1,-2),[3],[-48,78,9],'hello world'] ret = sorted(li,key = len) print(ret) 结果: [[3], (1, -2), [-48, 78, 9], 'hello world'] 4.请把以下函数变成匿名函数 def add(x,y): return x+y 改成匿名函数: ret = lambda x,y:x+y 5. 下面程序的输出结果是: d = lambda p:p*2 t = lambda p:p*3 x = 2 x = d(x) x = t(x) x = d(x)