问题描述
我正在尝试列出列表a
中所有NaNs
的索引.
I am trying to make a list with the index of all NaNs
in the list a
.
问题在于列表ind
中没有任何内容.如果我放置c
这样的随机字符串而不是NaN
,则可以正常工作.
The problem is that the list ind
doesn't fill with anything. It works if instead of NaN
, I put a random string like c
.
import numpy as np
a=[1, 2, 3, 4, np.nan, np.nan, 2, np.nan]
ind=[]
for i in range(0,len(a)):
if a[i]==float("NaN"):
ind.append(i)
print ind
推荐答案
如果您使用的是NumPy,则应该真正开始使用数组,并摆脱在Python级别手动循环的习惯.手动循环通常比让NumPy处理事情要慢大约100倍,并且浮点列表占用的内存大约是数组的4倍.
If you're using NumPy, you should really start using arrays, and get out of the habit of manually looping at Python level. Manual loops are typically about 100 times slower than letting NumPy handle things, and lists of floats take about 4 times the memory of an array.
在这种情况下,NumPy可以很简单地为您提供一系列NaN索引:
In this case, NumPy can give you an array of NaN indices quite simply:
ind = numpy.where(numpy.isnan(a))[0]
numpy.isnan
给出了一个数组告诉a
的哪些元素为NaN的布尔值. numpy.where
给出了一系列True
元素,但包装在1元素元组中,以与多维数组上的行为保持一致,因此[0]
从元组中提取数组.
numpy.isnan
gives an array of booleans telling which elements of a
are NaN. numpy.where
gives an array of indices of True
elements, but wrapped in a 1-element tuple for consistency with the behavior on multidimensional arrays, so [0]
extracts the array from the tuple.
当a
是列表时,此方法有效,但您实际上应该使用数组.
This works when a
is a list, but you should really use arrays.
您的尝试失败,因为NaN值彼此不相等或彼此不相等:
Your attempt fails because NaN values aren't equal to each other or themselves:
>>> numpy.nan == numpy.nan
False
>>> numpy.nan == float('nan')
False
NaN的设计目的是为了算法上的方便,使x != x
在生成要比较的NaN的环境中比较麻烦,并且因为NaN具有很少使用的 payload 在不同的NaN之间可能有所不同的成分.
NaN was designed this way for algorithmic convenience, to make x != x
a simple check for NaN values in environments where producing a NaN to compare against is awkward, and because NaNs have a rarely-used payload component that may be different between different NaNs.
另一个答案建议使用is numpy.nan
测试,但这是错误的并且不可靠.仅当您的NaN恰好是特定对象numpy.nan
时才有效,这种情况很少发生:
The other answer recommends an is numpy.nan
test, but this is buggy and unreliable. It only works if your NaNs happen to be the specific object numpy.nan
, which is rarely the case:
>>> float('nan') is numpy.nan
False
>>> numpy.float64(0)/0 is numpy.nan
__main__:1: RuntimeWarning: invalid value encountered in double_scalars
False
>>> numpy.array([numpy.nan])[0] is numpy.nan
False
依靠is numpy.nan
支票,它们会咬你.
这篇关于如何获取列表中所有NaN的所有索引?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!