python结构中的向量

时间:2010-12-20 00:10:52

标签: c++ 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模块,因为我以前从未使用它。会创建一个更好的课程吗?

2 个答案:

答案 0 :(得分:2)

您的问题并不清楚,但如果您只想在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

答案 1 :(得分:1)

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