我已经决定学习Python。我在C++中有一个背景,所以有一些挑战让我的大脑围绕着Python进行包装。我的问题是:Python等价于C++邻接表
我在C++中有以下语法,我正在寻找Python中的等价物。这只是一小段代码。我不确定我是否应该使用列表或Dicts。
int main()
{
...some code...
int** matrix = 0;
buildmatrix(vertices,matrix);
...some more code...
return EXIT_SUCCESS;
}
void buildmatrix(int& vertices,int** &matrix)
{
cout <<"Enter # of vertices -> ";
cin >>vertices;
matrix = new int* [vertices];
for(int i=0; i<vertices; i++)
{
matrix[i] = new int[vertices];
}
...some more code...
}
简而言之,这将构建一个数组指针数组。使它看起来像一个矩阵。将此代码转换为Python时使用的最佳数据结构和方法是什么?
因此,Python中的列表可以做指针数组(即动态二维数组)的指针? –
是的,你可以很容易地得到一个列表清单,这是与你的C++代码最相似的。 –