python生成随机不重复的整数,用random中的sample index = random.sample(range(0,10),10) 上面是生成不重复的10个从1~10的整数 python生成完全随机的整数,用numpy中的random.randint index = np.random.randint(0,10,size=10) 生成的是可能会重复的10个从0~10的整数
刚开始学python时候,发现一个很迷惑的现象,一直到看了源码后才知道了: >>> a=6 >>> b=6 >>> a is b True 想用同样的参数初始化两个对象,结果却是,这两个对象其实是同样的对象????逗我呢? >>> a=666 >>> b=666 >>> a is b False 这又是怎么回事?为什么现在又是False了??? 这些不同,主要来自于python中对待小整数和大整数的
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
/********************************************************************* * Author : Samson * Date : 09/19/2014 * Test platform: * Linux ubuntu 3.2.0-58-generic-pae * GNU bash, version 4.2.39 * ***********************
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