filter与compress

values = ['1', '2', '-3', '-', '4', 'N/A', '5']


def isNum(s):
    try:
        int(s)
        return True
    except:
        return False


print(list(filter(lambda i: isNum(i), values)))
# 简写
print(list(filter(isNum, values)))  # ['1', '2', '-3', '4', '5']

from itertools import compress

# 压缩?
print(list(compress(values, [isNum(i) for i in values])))  # ['1', '2', '-3', '4', '5']
上一篇:hive的数据压缩


下一篇:LeetCode 面试题 01.06. Compress String LCCI