numpy.arange(25).reshape((5, 5)) # create a 1-d range and reshapenumpy.array(range(25)).reshape((5, 5)) # pass a Python range and reshapenumpy.array([5] * 25).reshape((5, 5)) # pass a Python list and reshapenumpy.empty((5, 5)) # allocate, but don't initializenumpy.ones((5, 5)) # initialize with ones
import copy
def ndlist(*args, init=0):dp = initfor x in reversed(args):dp = [copy.deepcopy(dp) for _ in range(x)]return dp
l = ndlist(1,2,3,4) # 4 dimensional list initialized with 0'sl[0][1][2][3] = 1
# 2D array/ matrix
# 5 rows, 5 colsrows_count = 5cols_count = 5
# create# creation looks reverse# create an array of "cols_count" cols, for each of the "rows_count" rows# all elements are initialized to 0two_d_array = [[0 for j in range(cols_count)] for i in range(rows_count)]
# index is from 0 to 4# for both rows & cols# since 5 rows, 5 cols
# usetwo_d_array[0][0] = 1print two_d_array[0][0] # prints 1 # 1st row, 1st col (top-left element of matrix)
two_d_array[1][0] = 2print two_d_array[1][0] # prints 2 # 2nd row, 1st col
two_d_array[1][4] = 3print two_d_array[1][4] # prints 3 # 2nd row, last col
two_d_array[4][4] = 4print two_d_array[4][4] # prints 4 # last row, last col (right, bottom element of matrix)
list 1: To store rowslist 2: Actual two-dimensional matrix
将整行存储在第一个列表中。完成后,将列表1附加到列表2中:
from random import randint
coordinates=[]temp=[]points=int(raw_input("Enter No Of Coordinates >"))for i in range(0,points):randomx=randint(0,1000)randomy=randint(0,1000)temp=[]temp.append(randomx)temp.append(randomy)coordinates.append(temp)
print coordinates
输出:
Enter No Of Coordinates >4[[522, 96], [378, 276], [349, 741], [238, 439]]
def matrix(rows, cols, start=0):return [[c + start + r * cols for c in range(cols)] for r in range(rows)]
assert matrix(2, 3, 1) == [[1, 2, 3], [4, 5, 6]]
# get the input rows and colsrows = int(input("rows : "))cols = int(input("Cols : "))
# initialize the listl=[[0]*cols for i in range(rows)]
# fill some random values in itfor i in range(0,rows):for j in range(0,cols):l[i][j] = i+j
# print the listfor i in range(0,rows):print()for j in range(0,cols):print(l[i][j],end=" ")
def inmatrix(m,n):#Start function and pass row and column as parametera=[] #create a blank matrixfor i in range(m): #Row inputb=[]#blank listfor j in range(n): # column inputelm=int(input("Enter number in Pocket ["+str(i)+"]["+str(j)+"] ")) #Show Row And column numberb.append(elm) #add value to b lista.append(b)# Add list to matrixreturn a #return Matrix
def Matrix(a): #function for print Matrixfor i in range(len(a)): #rowfor j in range(len(a[0])): #columnprint(a[i][j],end=" ") #print value with spaceprint()#print a line After a row print
m=int(input("Enter number of row")) #input rown=int(input("Enter number of column"))a=inmatrix(m,n) #call input matrix function
print("Matrix is ... ")
Matrix(a) #print matrix function
import numpy as np
# Generate a random matrix of floatsnp.random.rand(cols,rows)
# Generate a random matrix of integersnp.random.randint(1, 10, size=(cols,rows))