python 列表过滤
- 2019 年 10 月 7 日
- 笔记
mylist = [1, 4, -5, 10, -7, 2, 3, -1] [n for n in mylist if n > 0] [1, 4, 10, 2, 3] [n for n in mylist if n < 0] [-5, -7, -1]
——-or values = ['1', '2', '-3', '-', '4', 'N/A', '5'] def is_int(val): try: x = int(val) return True except ValueError: return False ivals = list(filter(is_int, values)) print(ivals)