今天在用写一段求和的代码时候,发现最后返回的是nan的结果,这段循环求和代码依次调用了三个函数,于是依次打印这三个函数的返回值,发现其中一个函数的返回值为nan,原来是因为这段函数里面没有相似的用户,所有返回结果为nan,于是我就想加一个判断条件,当结果为空时,跳过本次循环: if res==nan: continue 本以为加上这一句话就可以了,于是重新运行了一下,oh,shit!!!!结果没有任何变化,以前是nan的还是一样没有跳过.于是我想到了我用到了Python的numpy库,会不会是n
想创建或测试正无穷.负无穷或NaN(非数字) 的浮点数 Python 并没有特殊的语法来表示这些特殊的浮点值,但是可以使用float() 来创建它们.比如: >>> a = float('inf') >>> b = float('-inf') >>> c = float('nan') >>> a inf >>> b -inf >>> c nan >>> 为了测试这些值的存在,使用m
118. Pascal's Triangle Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5,Return [ [1], [1,1], [1,2,1], [1,3,3,1], [1,4,6,4,1] ] class Solution(object): def generate(self, numRows): """ :type numR
from array import array from random floats = array('d',random((for i in range(10**7)) fp = open('floats.bin','wb') floats.tofile(fp) fp.close() floats2 = array('d') fp1 = open('floats.bin','rb') floats2.fromfile(fp1,10**7) fp1.close()
python中通常情况下for循环会枚举各个元素不会访问下标,例如: l = [1,2,4,6] for val in l: print l 但是有时候我们会需要在便利数组的同时访问下标,这时候可以借助于enumerate函数来实现,例如: l = [1,2,3] for index,val in enumerate(l): print 'index is %d, val is %d' % (index,val)
Attributes of numpy.ndarray: numpy.ndarray.shape: Dimensions (height, width, ...) numpy.ndarray.ndim: No. of dimensions = len(shape) numpy.ndarray.size: Total number of elements numpy.ndarray.dtype: Datatype import numpy as np def array(): a = np.ran
最近,处理两个矩阵的点除,得到结果后,再作其他的计算,发现有些内置的函数不work:查看得到的数据,发现有很多nan和inf,导致python的基本函数运行不了,这是因为在除的过程中分母出现0的缘故.为了将结果能够被python其他函数处理,尤其numpy库,需要将nan,inf转为python所能识别的类型.这里将nan,inf替换0作为例子. 1. 代码 import numpy as np a = np.array([[np.nan, np.nan, 1, 2], [np.inf, np.