python结构中的向量

问题描述:

有没有一种方法可以将结构中的向量存储在python中?具体来说,即时尝试做:python结构中的向量

struct data{ 
    string name   // name of event 
    string location  // location of event 
    vector <int> times // times the event happens in 24 hour format 
}; 

即时通讯不知道如何做到这一点与structs模块,因为ive从来没有使用过它。会创建一个班级做得更好吗?

+0

你想构建和Python的通过这些结构中的一个C++? – 2010-12-20 04:22:23

从您的问题中不清楚,但如果您只是想在Python中使用类似的结构,则可以使用list代替vector<int>

data = ('John','Los Angeles, CA',[1,2,3,4,5,6]) 

另一种方法是使用namedtuple

>>> import collections 
>>> Data = collections.namedtuple('Data','name location times') 
>>> data = Data('John','Los Angeles',[1,2,3,4]) 
>>> data.name 
'John' 
>>> data.location 
'Los Angeles' 
>>> data.times 
[1, 2, 3, 4] 
>>> data.times[0] 
1 

Python的结构模块不能处理std::vector,它具有实现定义的内部结构。您需要提供某种包装(例如,SWIGBoost.Python)以将此结构公开给Python。