进阶 对传入的数据进行分类

def bifurcate_by(lst, fn):
    print(lst)
    # [‘beep‘, ‘boop‘, ‘foo‘, ‘bar‘]

    print(fn(‘baby‘))
    # True

    print(fn(‘abc‘))
    # False

    print([
    [x for x in lst if fn(x)],
    [x for x in lst if not fn(x)]
    ])

bifurcate_by(
  [‘beep‘, ‘boop‘, ‘foo‘, ‘bar‘], lambda x: x[0] == ‘b‘
)
# [[‘beep‘, ‘boop‘, ‘bar‘], [‘foo‘]]

进阶 对传入的数据进行分类

相关推荐