PDA

View Full Version : Is there an equivlent data type as the "struct" in C


Zhi
04-02-2011, 09:20 AM
Hi

I want to create a "struct" type variable array in Vizard. Each element in the array contains 5 sub-elements with different data type (i.e. string, int, float, etc.). Any suggestion? Thanks

Zhi

Zhi
04-02-2011, 01:38 PM
I checked the Python website. It seems a similar structure to the "struct" in C would be the "list (http://www.tutorialspoint.com/python/python_lists.htm)" or "dict (http://www.tutorialspoint.com/python/python_dictionary.htm)". The "list" is more like an array in C, while the "dict" is similar to the "struct". However, I don't know how to combine the list and dict to make a struct array. Can anyone help me?

Zhi

farshizzo
04-02-2011, 01:56 PM
It sounds like you just want an array of custom class objects. The following code shows how to create a simple class and add instances of it to a list:
class MyStruct(object):
def __init__(self,my_int,my_float,my_string):
self.my_int = my_int
self.my_float = my_float
self.my_string = my_string

array = []
array.append( MyStruct(0,0.0,'foo') )
array.append( MyStruct(1,1.0,'bar') )

print array[0].my_string
Let me know if this isn't what you are looking for.

Zhi
04-02-2011, 03:40 PM
That's exactly what I want. Thank you.

Zhi