[Tutor] append question

Kent Johnson kent37 at tds.net
Mon Jul 6 12:50:58 CEST 2009


> 2009/7/6 Steven Buck <buckstec at gmail.com>:

>> # I call my data set the psid (Panel Study of Income Dynamics)
>> # In Stata this would look like and NXK matrix (N observations and K
>> variables)
>> psid=Reader(file('data3.dta'))
>>
>> # I gather this next just creates a list of the variable names.
>> varnames=[x.name for x in psid.variables()]

Yes, but psid.variables() is already a list of variable names, so you
could just say
varnames = psid.variables()

>>  From here, I'd like Python to identify the Nx1 vectors (or n-tuples) that
>> correspond to the varnames list defined above.  I can't seem grab the
>> vectors representing age, wage, etc..  I've tried things like
>> age, psid['age'], psid.age.  My last email was an attempt to create the
>> vectors myself, although the Reader module puts the data in a dictionary
>> structure so the append command I was trying to use doesn't work.

psid.dataset() is the list of lists that you need to start. Try this:

data = psid.dataset()
ages = [ item[0] for item in data ]
wages = [ item[1] for item in data ]

This way of making a list is called a list comprehension, you can read
about them here:
http://docs.python.org/tutorial/datastructures.html#list-comprehensions

Kent


More information about the Tutor mailing list