2010-12-20 84 views
2

有沒有一種方法可以將結構中的向量存儲在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

回答

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

Python的結構模塊不能處理std::vector,它具有實現定義的內部結構。您需要提供某種包裝(例如,SWIGBoost.Python)以將此結構公開給Python。