创建二维数组

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));
posted @ 2020-08-26 17:01  kite97  阅读(189)  评论(0编辑  收藏  举报