Re: [Tutor] append question
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 roadier...@googlemail.comwrote: 2009/7/5 Steven Buck bucks...@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/ ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] append question
2009/7/6 Steven Buck bucks...@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 ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] append question
Hi Python Tutors: I have a data structure that looks like: test=[[1,2,3],[4,5,6],[7,8,9]] I want to define a new variable that captures the second element of each sublist from above: testvar2 = [] Next I try to capture the aforementioned elements: 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 ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] append question
Read your error message... It highlighted the first line of your for loop ansd said ints aren't iterable. len(list) returns an integer. You want a list of items... for i in range(len(list)): On 7/5/09, Steven Buck bucks...@gmail.com wrote: Hi Python Tutors: I have a data structure that looks like: test=[[1,2,3],[4,5,6],[7,8,9]] I want to define a new variable that captures the second element of each sublist from above: testvar2 = [] Next I try to capture the aforementioned elements: 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 -- Sent from my mobile device ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] append question
2009/7/5 Steven Buck bucks...@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 ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] append question
In [1]: test=[[1,2,3],[4,5,6],[7,8,9]] In [3]: testvar2 = [] In [16]: for i in range(len(test)): : testvar2.append(test[i][1]) : : In [17]: testvar2 Out[17]: [2, 5, 8] Robert On Sun, 2009-07-05 at 15:57 -0700, Steven Buck wrote: Hi Python Tutors: I have a data structure that looks like: test=[[1,2,3],[4,5,6],[7,8,9]] I want to define a new variable that captures the second element of each sublist from above: testvar2 = [] Next I try to capture the aforementioned elements: 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 ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] append question
2009/7/6 Steven Buck bucks...@gmail.com: 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 Sorry about suggesting this was homework... I noticed the word University, but not the line above it saying PhD student... If you're new to python, the standard path we recommend is to take an online tutorial. Dive into Python (http://www.diveintopython.org/) has been recommended for people with some programming experience, and Think Python for those without, although I'm sure that if you ask 10 members of this list, you'll get 20 different suggestions. Real veterans get pointed at the standard libraries, which are extremly well commented in the most part. I personally learned from the tutorial in the python documentation, but it does leave a fair bit of the thought processes involved in programming out. See what I mean? One person, three recomendations. -- Richard Roadie Rich Lovely, part of the JNP|UK Famile www.theJNP.com ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor