问题
Array A looks like this: [1, -inf, 2, 3, inf, -60.2]
Array B should look like this: [1, 2, 3, -60.2]
How can I make array B from array A without infinities included in Python 2.7?
回答1:
B = filter(lambda x: abs(x) != float('inf'), A)
回答2:
B = [x for x in A if not math.isinf(x)]
回答3:
Did you mean:
>>> 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]
?
回答4:
The easiest one:
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)
Not a one-line solution, but clear and simple.
回答5:
you can define inf by
inf = 1e400
来源:https://stackoverflow.com/questions/6841837/how-can-i-make-array-b-from-array-a-without-infinities-included-in-python-2-7