How can I make array B from array A without infinities included in Python 2.7?

為{幸葍}努か 提交于 2019-12-10 12:23:18

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!