Python练习题答案: 名单单子内绑定【难度:2级】--景越Python编程实例训练营,1000道上机题等你来挑战

名单单子内绑定【难度:2级】:

答案1:

def bind(lst, func):
    return [y for x in lst for y in func(x)]

答案2:

bind=lambda l,f:sum(map(f,l),[])

答案3:

def bind(lst,func):
    return reduce(lambda x, y: x+func(y), lst, [])

答案4:

import itertools

def bind(lst, func):
    return list(itertools.chain.from_iterable(map(func, lst)))

答案5:

def bind(lst,func):
    result = []
    for x in lst:
        result += func(x)
    return result​

答案6:

def bind(lst,func):
  return sum(map(func, lst), []);

答案7:

def bind(lst,func):
    return sum(map(func,lst),[])

答案8:

from itertools import chain
def bind(lst, func):
  return list(chain.from_iterable([func(x) for x in lst])) # sometimes you just have to love python​

答案9:

bind=lambda lst,func: reduce(lambda a,b:a+b, map(func,lst[:]), [])

答案10:

bind = lambda l,f: [e for i in [f(x) for x in l] for e in i]

答案11:

bind = lambda lst,func: [o for e in lst for o in func(e)]



Python基础训练营景越Python基础训练营QQ群

Python练习题答案: 名单单子内绑定【难度:2级】--景越Python编程实例训练营,1000道上机题等你来挑战_第1张图片
欢迎各位同学加群讨论,一起学习,共同成长!

你可能感兴趣的:(Python编程进阶练习题)