Python二维数组 发表于 2020-11-28 二维数组初始化First1a=[[1,2,3],[2,4,6]] Second123456789101112a=[[0]*3,[1]*3]print(a)##output:[[0,0,0],[1,1,1]]##尽量不用a=[[0]*3]*2print(a)##output:[[0,0,0],[0,0,0]]a[1][0]=1print(a)##output:[[1,0,0],[1,0,0]]##会同时改变 Third123456789101112131415161718a=[[0]*3,[1]*3]count=0for row in range(2): for col in range(3): a[row][col]=countcount+=1print(a)##output[[0,1,2],[3,4,5]]a=[]count=0for row in range(2): b=[] for col in range(3): b.append(count) count+=1 a.append(b)print(a)##output[[0,1,2],[3,4,5]] Forth1234567891011121314151617a=[[]for row in range(2)]print(a)##output:[[],[]]a=[[col + row*10 for col in range(3)]for row in range(2)]## col row## [[0+0*10=0,1+0*10=1,2+0*10=2],[0+1*10=10,1+1*10=11,2+1*10=12]]print(a)##output:[[0,1,2],[10,11,12]]b=[]a=list(map(int,input("").split()))b.append(a)print(b)##input:1 3 5##output:[[1,3,5]]