在Python中初始化一个固定大小的数组

``int x[5]; /* declared without adding elements*/` `

` `>>> lst = [None] * 5 >>> lst [None, None, None, None, None]` `

` `a = numpy.empty(n, dtype=object)` `

` `Object[] a = new Object[n];` `

` `from numpy import ndarray a = ndarray((5,),int)` `

` `>>> d = [ [ None for y in range( 2 ) ] for x in range( 2 ) ] >>> d [[None, None], [None, None]] >>> d[0][0] = 1 >>> d [[1, None], [None, None]]` `

` `>>> d = [ [ None ] * 2 ] * 2 >>> d [[None, None], [None, None]] >>> d[0][0] = 1 >>> d [[1, None], [1, None]]` `
` `>>> import numpy >>> x = numpy.zeros((3,4)) >>> x array([[ 0., 0., 0., 0.], [ 0., 0., 0., 0.], [ 0., 0., 0., 0.]]) >>> y = numpy.zeros(5) >>> y array([ 0., 0., 0., 0., 0.])` `

` `t = input("") x = [None]*t y = [[None]*t]*t for i in range(1, t+1): x[i-1] = i; for j in range(1, t+1): y[i-1][j-1] = j; print x print y` `

` `2 [1, 2] [[1, 2], [1, 2]]` `

` `x = [0]*10` `

` `>>> n = 5 #length of list >>> list = [None] * n #populate list, length n with n entries "None" >>> print(list) [None, None, None, None, None] >>> list.append(1) #append 1 to right side of list >>> list = list[-n:] #redefine list as the last n elements of list >>> print(list) [None, None, None, None, 1] >>> list.append(1) #append 1 to right side of list >>> list = list[-n:] #redefine list as the last n elements of list >>> print(list) [None, None, None, 1, 1] >>> list.append(1) #append 1 to right side of list >>> list = list[-n:] #redefine list as the last n elements of list >>> print(list) [None, None, 1, 1, 1]` `

` `>>> n = 5 #length of list >>> list = [] # create list >>> print(list) [] >>> list.append(1) #append 1 to right side of list >>> list = list[-n:] #redefine list as the last n elements of list >>> print(list) [1]` `

` `>>> list.append(1) #append 1 to right side of list >>> list = list[-n:] #redefine list as the last n elements of list >>> print(list) [1,1,1,1]` `

5以及随后的所有：

` `>>> list.append(1) #append 1 to right side of list >>> list = list[-n:] #redefine list as the last n elements of list >>> print(list) [1,1,1,1,1]` `

` `class fixedSizeArray(object): def __init__(self, arraySize=5): self.arraySize = arraySize self.array = [None] * self.arraySize def __repr__(self): return str(self.array) def __get__(self, instance, owner): return self.array def append(self, index=None, value=None): print "Append Operation cannot be performed on fixed size array" return def insert(self, index=None, value=None): if not index and index - 1 not in xrange(self.arraySize): print 'invalid Index or Array Size Exceeded' return try: self.array[index] = value except: print 'This is Fixed Size Array: Please Use the available Indices' arr = fixedSizeArray(5) print arr arr.append(100) print arr arr.insert(1, 200) print arr arr.insert(5, 300) print arr` `

OUTPUT：

` `[None, None, None, None, None] Append Operation cannot be performed on fixed size array [None, None, None, None, None] [None, 200, None, None, None] This is Fixed Size Array: Please Use the available Indices [None, 200, None, None, None]` `

` `import numpy as np x= np.zeros(5,str) print x` `

` `['' '' '' '' '']` `

` `with open(FILENAME, 'rb') as f: data = bytearray(os.path.getsize(FILENAME)) f.readinto(data)` `