Cihan Cihan - 3 months ago 17
C++ Question

unordered_map<int, vector<float>> equivalent in Python

I need a structure in Python which maps an integer index to a vector of floating point numbers. My data is like:

[0] = {1.0, 1.0, 1.0, 1.0}
[1] = {0.5, 1.0}


If I were to write this in C++ I would use the following code for define / add / access as follows:

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];


What is the equivalent structure of this in Python?

ChE ChE
Answer

I would go for a dict with integers as keys and list as items, e.g.

m = dict()
m[0] = list()
m[0].append(1.0)
m[0].append(0.5)
m[13].append(13.0)

if it is not too much data

Comments