[英]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.我需要 Python 中的结构,它将 integer 索引映射到浮点数向量。 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:如果我要在 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];
What is the equivalent structure of this in Python? Python中这个等价的结构是什么?
How about dictionary and lists like this: 这样的字典和列表怎么样:
>>> d = {0: [1.0, 1.0, 1.0, 1.0], 1: [0.5, 1.0]}
>>> d[0]
[1.0, 1.0, 1.0, 1.0]
>>> d[1]
[0.5, 1.0]
>>>
The key can be integers and associated values can be stored as a list. 键可以是整数,并且关联的值可以存储为列表。 Dictionary in Python is a hash map and the complexity is amortized
O(1)
. Python中的Dictionary是一个哈希图,其复杂度为
O(1)
。
A dictionary of this format -> { (int) key : (list) value }
这种格式的字典 ->
{ (int) key : (list) value }
d = {} # Initialize empty dictionary.
d[0] = [1.0, 1.0, 1.0, 1.0] # Place key 0 in d, and map this array to it.
print d[0]
d[1] = [0.5, 1.0]
print d[1]
>>> [1.0, 1.0, 1.0, 1.0]
>>> [0.5, 1.0]
print d[0][0] # std::cout <<vertexWeights[0][0];
>>> 1.0
I would go for a dict
with integers as keys and list
as items, eg 我会去一个
dict
以整数作为键, list
作为项目,例如
m = dict()
m[0] = list()
m[0].append(1.0)
m[0].append(0.5)
m[13] = list()
m[13].append(13.0)
if it is not too much data 如果不是太多数据
In python we can this data structure as Dictionary.在 python 中,我们可以将此数据结构作为字典。 Dictionaries are used to store data values in key:value pairs.
字典用于将数据值存储在键:值对中。 Example for Dictionary: mydict = { "brand": "Ford", "model": "Mustang", "year": 1964 } we can also perform various operations like add, remove.
字典示例: mydict = { "brand": "Ford", "model": "Mustang", "year": 1964 } 我们还可以执行各种操作,例如添加、删除。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.