配列Aは次のようになります:[1、-inf、2、3、inf、-60.2]
配列Bは次のようになります:[1、2、3、-60.2]
Python 2.7に無限大が含まれていない配列Aから配列Bを作成するにはどうすればよいですか?
B = [x for x in A if not math.isinf(x)]
B = filter(lambda x: abs(x) != float('inf'), A)
もしかして:
>>> inf = float('inf');
>>> import math
>>> print filter(lambda x: not math.isinf(x), [1, -inf, 2, 3, inf, -60.2])
[1, 2, 3, -60.200000000000003]
?
最も簡単なもの:
arrayA = [1, float('-inf'), 2, 3, float('inf'), -60.2]
arrayB = []
for item in arrayA:
if item != float('inf') and item != float('-inf'):
arrayB.append(item)
1行のソリューションではありませんが、明確でシンプルです。
あなたはinfを定義することができます
inf = 1e400