>>> F = lambda v: F(v[1:]) if v and v[0] == 'Home' else v
>>> L = ['Home', 'Home', 'Home and Garden', 'Home', 'Kitchen']
>>> F(L)
['Home and Garden', 'Home', 'Kitchen']
然而,这将是低效的,Python已经有了这样的工具:
>>> from itertools import dropwhile
>>> L = ['Home', 'Home', 'Home and Garden', 'Home', 'Kitchen']
>>> list(dropwhile(lambda x: x == 'Home', L))
['Home and Garden', 'Home', 'Kitchen']
或者:
>>> from functools import partial
>>> from operator import eq
>>> from itertools import dropwhile
>>> L = ['Home', 'Home', 'Home and Garden', 'Home', 'Kitchen']
>>> list(dropwhile(partial(eq, "Home"), L))
['Home and Garden', 'Home', 'Kitchen']
也许这就是你的
lambda
的目的:然而,这将是低效的,Python已经有了这样的工具:
或者:
这应该更快,bur需要更多的进口
相关问题 更多 >
编程相关推荐