python-small-examples/md/72.md at master · data-python/python-small-examples

File metadata and controls

20 lines (15 loc) · 300 Bytes

@author jackzhenguo
@desc 
@date 2019/4/15

72 按条件分组

def bif_by(lst, f):
    return [ [x for x in lst if f(x)],[x for x in lst if not f(x)]]

测试举例:

records = [25,89,31,34] 
bif_by(records, lambda x: x<80) # [[25, 31, 34], [89]]