我想定义一个没有初始化长度的二维数组,如下所示:
Matrix = [][]
但这给出了一个错误:
IndexError:列表索引超出范围
我想定义一个没有初始化长度的二维数组,如下所示:
Matrix = [][]
但这给出了一个错误:
IndexError:列表索引超出范围
当前回答
通常,go to模块是NumPy:
import numpy as np
# Generate a random matrix of floats
np.random.rand(cols,rows)
# Generate a random matrix of integers
np.random.randint(1, 10, size=(cols,rows))
其他回答
下面是一个用于初始化列表列表的简短符号:
matrix = [[0]*5 for i in range(5)]
不幸的是,将其缩短为5*[5*[0]这样的长度并不能真正起作用,因为您最终会得到相同列表的5个副本,因此当您修改其中一个副本时,它们都会发生变化,例如:
>>> matrix = 5*[5*[0]]
>>> matrix
[[0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0]]
>>> matrix[4][4] = 2
>>> matrix
[[0, 0, 0, 0, 2], [0, 0, 0, 0, 2], [0, 0, 0, 0, 2], [0, 0, 0, 0, 2], [0, 0, 0, 0, 2]]
输入矩阵和打印的用户定义功能
def inmatrix(m,n):
#Start function and pass row and column as parameter
a=[] #create a blank matrix
for i in range(m): #Row input
b=[]#blank list
for j in range(n): # column input
elm=int(input("Enter number in Pocket ["+str(i)+"]["+str(j)+"] ")) #Show Row And column number
b.append(elm) #add value to b list
a.append(b)# Add list to matrix
return a #return Matrix
def Matrix(a): #function for print Matrix
for i in range(len(a)): #row
for j in range(len(a[0])): #column
print(a[i][j],end=" ") #print value with space
print()#print a line After a row print
m=int(input("Enter number of row")) #input row
n=int(input("Enter number of column"))
a=inmatrix(m,n) #call input matrix function
print("Matrix is ... ")
Matrix(a) #print matrix function
你应该列出一个列表,最好的方法是使用嵌套的理解:
>>> matrix = [[0 for i in range(5)] for j in range(5)]
>>> pprint.pprint(matrix)
[[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0]]
在您的[5][5]示例中,您正在创建一个包含整数“5”的列表,并尝试访问其第5项,这自然会引发IndexError,因为没有第5项:
>>> l = [5]
>>> l[5]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
IndexError: list index out of range
从技术上讲,您正在尝试对未初始化的数组进行索引。在添加项目之前,必须先用列表初始化外部列表;Python调用此“列表理解”。
# Creates a list containing 5 lists, each of 8 items, all set to 0
w, h = 8, 5
Matrix = [[0 for x in range(w)] for y in range(h)]
#您现在可以向列表中添加项目:
Matrix[0][0] = 1
Matrix[6][0] = 3 # error! range...
Matrix[0][6] = 3 # valid
注意,矩阵是“y”地址主,换句话说,“y索引”在“x索引”之前。
print Matrix[0][0] # prints 1
x, y = 0, 6
print Matrix[x][y] # prints 3; be careful with indexing!
尽管您可以根据自己的意愿命名它们,但我这样看是为了避免索引中可能出现的一些混淆,如果您对内部和外部列表都使用“x”,并且希望使用非方形矩阵。
为便于阅读而重写:
# 2D array/ matrix
# 5 rows, 5 cols
rows_count = 5
cols_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 0
two_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
# use
two_d_array[0][0] = 1
print two_d_array[0][0] # prints 1 # 1st row, 1st col (top-left element of matrix)
two_d_array[1][0] = 2
print two_d_array[1][0] # prints 2 # 2nd row, 1st col
two_d_array[1][4] = 3
print two_d_array[1][4] # prints 3 # 2nd row, last col
two_d_array[4][4] = 4
print two_d_array[4][4] # prints 4 # last row, last col (right, bottom element of matrix)