我需要一个Python结构,它将整数索引映射到浮点数的向量。我的数据是这样的:unordered_map <int,vector <float>>在Python中相当于
[0] = {1.0, 1.0, 1.0, 1.0}
[1] = {0.5, 1.0}
如果我用C写这篇++我会用下面的代码定义/添加/访问如下:
std::unordered_map<int, std::vector<float>> VertexWeights;
VertexWeights[0].push_back(0.0f);
vertexWeights[0].push_back(1.0f);
vertexWeights[13].push_back(0.5f);
std::cout <<vertexWeights[0][0];
什么是这等同结构蟒蛇?
在c + +中,如果没有一个键让我们说d [15],那么它会自动创建。但在Pyhton中,我得到了一个关键错误。有没有办法解决这个问题? – Cihan
Yep,C++和python在这方面以相同的方式工作。更新了我的答案。 Python的字典文字本质上是一个无序的映射。 @Cihan – ospahiu
@Cihan尝试['''collections.defaultdict(list)'''](https://docs.python.org/3/library/collections.html#collections.defaultdict)。 – wwii