what is the python equivelant of this?

Calvelo Daniel dcalvelo at pharion.univ-lille2.fr
Thu Oct 12 13:57:58 EDT 2000


jschmitt at vmlabs.com wrote:
: typedef struct
: {
:     int   field1;
:     char* field2;
: } RECORD;
:
: RECORD records[] =
: {
:     { 10, "gumby" },
:     { 20, "barny" }
:     /* etc */
: };

Using a Python "list":

records = [ (10,"gumby"),
            (20,"barny") # etc
          ]

: or how about

: RECORD* recordarray = malloc( sizeof(RECORD) * 10 );
: memset( recordarray0, sizeof(RECORD) * 10 );

No need for allocation. It is done as the list grows.
For instance:

records.append( (30,"hwdy") )

If you need to cut back the list, you don't have to care about deallocation:

records = records[:2] # take the first two records out

If your records are to be retrieved by one of the fields, you might also use 
this field as keys for a "dictionary":

records = { 10:"gumby" , 20:"barny" }

>From there, if you wish to have all the field2's:

records.values()

all the records' field1's:

records.keys()

and to retrieve a particular field2 knowing its field1:

records[20] # returns "gumby"

Try to browse the tutorial. Python is fresh air+water+fire coming from C.

HTH, DCA

-- Daniel Calvelo Aros
     calvelo at lifl.fr



More information about the Python-list mailing list