标签:values except com try tool tools val code false
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‘]
标签:values except com try tool tools val code false
原文地址:https://www.cnblogs.com/fly-book/p/13396101.html