[Tutor] append question
Steven Buck
buckstec at gmail.com
Mon Jul 6 05:06:37 CEST 2009
Thanks for the previous responses. This isn't homework--I'm beyond
coursework, although I am a newbie to Python (and I've never had to do much
real programming since I've just used Stata for econometric analysis). I'm
testing Python as a more powerful alternative to Stata.
I've learned from the responses I received, although now see my problem
differently. The data structure I have uses a dictionary and know now
that the append command doesn't work. Having said that, perhaps my
variables of interest have already been created--perhaps I just don't know
how to identify them. I've been using some borrowed code to get me started;
my modified version is below:
import sys
# The modules below help me get a .dta file into Python. # Although I'm not
sure what form they take; I suppose a list of lists???
from StataTools import Reader
from StataTypes import MissingValue
# 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()]
# It's not clear what these next two lines gain me.
labels=psid.file_headers()['vlblist']
Labels=dict(zip(varnames,labels))
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.
Hopefully once I learn to create and call on my own vectors and matrices
I'll be better off--I'm comfortable working with these in MATLAB and Stata.
Bottom line: Given the above data I've imported/extracted from Stata .dta
file, how do I create an Nx1 vector which I call 'age'?
Thanks for your patience with this newbie.
Steve
On Sun, Jul 5, 2009 at 5:19 PM, Rich Lovely <roadierich at googlemail.com>wrote:
> 2009/7/5 Steven Buck <buckstec at gmail.com>:
> >>>> for i in len(test): > testvar2.append(test[i][2])
> >
> > I want testvar2 = [2,5,8] but instead I get the following error message:
> >
> > Traceback (most recent call last):
> > File "<pyshell#34>", line 1, in <module>
> > for i in len(test):
> > TypeError: 'int' object is not iterable
> >
> > Any insight would be appreciated.
> > Thanks
> > Steve
> > --
> > Steven Buck
> > Ph.D. Student
> > Department of Agricultural and Resource Economics
> > University of California, Berkeley
>
>
> This sounds like a homework assignment, and we're not supposed to give out
> answers to homework.
>
> The error message and the docs explain what you're doing wrong if you
> take a moment to look.
> from http://www.python.org/doc/2.6/reference/compound_stmts.html#for
>
> """for_stmt ::= "for" target_list "in" expression_list ":" suite
> ["else" ":" suite]
>
> The expression list is evaluated once; it should yield an iterable
> object. An iterator is created for the result of the expression_list.
> The suite is then executed once for each item provided by the
> iterator, in the order of ascending indices. Each item in turn is
> assigned to the target list using the standard rules for assignments,
> and then the suite is executed."""
>
> As Luke said, len returns an int, which as your error tells you, is
> not iterable. From the same page:
> """The for statement is used to iterate over the elements of a
> sequence (such as a string, tuple or list) or other iterable
> object:"""
>
> Therefore you have an iterable, there is no need to try and construct a new
> one.
>
> Does that help?
>
> It is extremly unpythonic to iterate over range(len(...)), as it adds
> in the overhead of two function calls, and ruins the readability of
> code. The latter is probably the most important of the two.
>
> An even more pythonic way to do this would be a list comprehension,
>
> http://www.python.org/doc/2.6/tutorial/datastructures.html#list-comprehensions
>
> If it's not homework, let us know, and we'll be more than willing to
> give you code if you still need it.
>
> --
> Richard "Roadie Rich" Lovely, part of the JNP|UK Famile
> www.theJNP.com <http://www.thejnp.com/>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20090705/52c50fcf/attachment.htm>
More information about the Tutor
mailing list