calccrypto
calccrypto

Reputation: 8991

vectors in python structs

is there a way to store vectors in structs in python? specifically, im trying to do:

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

im not sure how to do it with the structs module, since ive never used it before. will creating a class do it better?

Upvotes: 2

Views: 828

Answers (2)

Mark Tolonen
Mark Tolonen

Reputation: 177971

It's not clear from your question, but if you just want a similar structure in Python you can use a list in place of the vector<int>.

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

Another alternative is to use a 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

Upvotes: 2

Marcelo Cantos
Marcelo Cantos

Reputation: 185970

Python's struct module cannot deal with std::vector, which has an implementation-defined internal structure. You will need to provide some kind of wrapping (e.g., SWIG or Boost.Python) to expose this structure to Python.

Upvotes: 1

Related Questions