创建二维数组
python
浅拷贝
dp = [[0] * 3 ]*3
dp[1][1] = 1
print(dp)
#[[0, 1, 0], [0, 1, 0], [0, 1, 0]]
列表推导式
dp = [[0] * 3 for j in range(3)]
dp[1][1] = 1
print(dp)
#[[0, 0, 0], [0, 1, 0], [0, 0, 0]]
dp = [[0 for i in range(3)] for j in range(3)] #里边那层是列,外边那层才是行
dp[1][1] = 1
print(dp)
#[[0, 0, 0], [0, 1, 0], [0, 0, 0]]
numpy
-
numpy创建零数组
import numpy as np dp = np.zeros((3,3),dtype = int) dp[1][1] = 1 print(dp) #[[0 0 0] # [0 1 0] # [0 0 0]]
-
numpy创建递增数组
import numpy as np dp = np.arange(12).reshape(4, 3) print(dp) #[[ 0 1 2] # [ 3 4 5] # [ 6 7 8] # [ 9 10 11]]
-
numpy创建空数组
import numpy as np dp = np.empty((3,3),dtype = int) print(dp) #[[ 1 0 8] 元素随机 # [ 0 8 0] # [ 8 0 1852796001]]
c++
Normal
int** arr = new int* [row];
for (int i = 0; i < row; ++i)
arr[i] = new int[col];
注意自己回收内存
for (int i = 0; i < row; ++i)
delete[]arr[i];
delete[]arr;
Vector
vector<vector<int>> arr(row,vector<int>(col,init_value));
保持学习,保持思考,保持对世界的好奇心!