[Tutor] optparse example
Hello list, I tried the optparse example from the python library reference and it doesn't seem to work..what am I doing wrong? I keep getting the "incorrect number of arguments" message although i use the correct number.. The example is here : http://docs.python.org/lib/optparse-putting-it-all-together.html if i edit the code and write: print parser.parse_args() I get: (, []) as you can see the argument list is empty I run the program as ./parser.py -q (i also tried 'python parser.py -q' but didnt help) Thank you in advance ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] optparse example
On Mon, Sep 22, 2008 at 7:16 AM, Tasos Latsas <[EMAIL PROTECTED]> wrote: > Hello list, > I tried the optparse example from the python library reference and it > doesn't seem to work..what am I doing wrong? > I keep getting the "incorrect number of arguments" message although i > use the correct number.. > > The example is here : > http://docs.python.org/lib/optparse-putting-it-all-together.html > > if i edit the code and write: > print parser.parse_args() > > I get: > (, []) > > as you can see the argument list is empty > I run the program as ./parser.py -q > (i also tried 'python parser.py -q' but didnt help) > > Thank you in advance > ___ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor > (I failed to send my original reply to the list) Notice the Usage message when you run the example: $ python parser.py -q Usage: parser.py [options] arg parser.py: error: incorrect number of arguments "-q" is an option, not an argument. If you look at the example, you add options: parser.add_option("-q", ... The arguments are everything left over. In this case, they are not used, but they are required. The following runs without issue: $ python parser.py junk $ python parser.py -v junk reading None... The bottom line is the example is just an example, not something you can use as is. -- Carlos Hanson ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] optparse example
On 9/22/08, Tasos Latsas <[EMAIL PROTECTED]> wrote: > Hello list, > I tried the optparse example from the python library reference and it > doesn't seem to work..what am I doing wrong? > I keep getting the "incorrect number of arguments" message although i > use the correct number.. Actually, you aren't using the correct number of arguments for the code as written. This line: if len(args) != 1: parser.error("incorrect number of arguments") checks to see if the arguments, not to be confused with the options, are greater than one. So, one way you might call the script would be: python parser.py somefile.txt Take care, Don ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Exhaustive Enumeration
Download links... 1) Think Python (about 240 pages)--Allen Downey and http://www.greenteapress.com/thinkpython/thinkpython.html > 2) A Byte of Python (about 100 pages) http://www.swaroopch.com/notes/Python#Download On Sun, 2008-09-21 at 16:31 -0400, Robert Berman wrote: > First, thank you for the explanation. > > I admire your desire to learn bull riding by jumping on the monster's > back. The problem with assignments based on a course is that many > professors and associates have learned the only way to insure student > class attendance is to obfuscate the assignment list. I think you may > be missing some important information available to anyone with the > class syllabus or the notes of an above average student. It could > also be that at this time this assignment might be more than you > should tackle. You certainly know more about your own skill level than > anyone else. > > The best possible suggestions I have to offer is to download two > python tutorial texts (neither of which is quick to read or > superficial in content). > > 1) Think Python (about 240 pages)--Allen Downey and > > 2) A Byte of Python (about 100 pages) > > I use a Byte of Python as a very good reference manual. Think Python > has a number of problems after each chapter as well as some problems > that will make your brain itch as you solve them. Think Python might > be a very good way to get into the functionalities that might make > your MIT teaser a bit more manageable. > > In any case, best of luck no matter which path you pursue. > > Robert > > > > [EMAIL PROTECTED] wrote: > > I'm actually not enrolled in the course; MIT puts some of its course > > materials available online as a general resource. I am out of school > > and trying to teach myself python on my own. I'm very much a > > beginner, and since I'm not privy to the lectures or notes from this > > course I have to fill in things from other sources. Basically, with > > respect to this problem, I'm at a loss as to how to approach it. My > > first thought was to use some sort of nested for loop structure to > > iterate through each possible state combination, but I don't think > > this is possible, since a for loop would just test for, for > > instance, (state 1 and 2, state 1 and 3, state 1 and 4, etc.) and > > not (state 1 and 3, state 1 and 2 and 3, etc.). I could maybe make > > it work for a very small number of states, but if you are taking 10 > > states as arguments I don't see a way this could work. Also, the way > > the question is asked seems to imply that the new code will go after > > the existing code, and also that it will be only about five lines. > > I'm guessing that maybe some kind of recursion is required but I > > really don't know, and recursion is a new concept to me as well. > > > > Thanks! > > > > Quoting Robert Berman <[EMAIL PROTECTED]>: > > > > > A very interesting problem. Given this is homework, I am > > > not > > > sure what it is you want. > > > > > > Do you want the solution coded and tested? > > > Do you want this to include two or three algorithms optimized for > > > speed as well as accuracy? > > > > > > I think the problem is well enough defined so that you do not > > > need > > > any clarification of what it is your professor wants from you. > > > > > > If you would like some help finding a solution set, perhaps you > > > would consider submitting some of your own solutions and > > > commentary > > > as to where your ideas are breaking down. Then, probably, a number > > > of > > > people will jump in to help you. > > > > > > Robert > > > > > > [EMAIL PROTECTED] wrote: This is from the MIT Opencourseware > > > intro > > > to computer science course, which I've been working my way > > > through. I > > > understand what needs to be done (I think), which is basically to > > > test each possibility and return the list of states with the most > > > electoral votes that can be paid for under the campaign budget. I > > > am > > > just at a loss as to how to do it, and I've been thinking about > > > it > > > all weekend. Here is the problem I am referring to. One assumption > > > is > > > that every $1 of money spent in a state translates into a popular > > > vote. Many thanks for any suggestions. Also, the next part of the > > > question asks to do the same thing using "Branch and Bound", which > > > I > > > also anticipate having trouble with. If I haven't described > > > things > > > sufficiently, the complete problem is available at: > > > > > > http://ocw.mit.edu/OcwWeb/Electrical-Engineering-and-Computer-Science/6-00Fall-2007/Assignments/index.htm > > > > > > , problem 5. > > > > > > Complete and test this function according to her specification > > > below. > > > Note that she was using exhaustive > > > enumeration to compute the result. Your friend assured you that > > > the > > > function needs no more than 5 additional > > > lines of code. > > > > > > #
[Tutor] array of different datatypes
I have looked (honestly!) and cannot see an array structure to allow different datatypes per column. I need a 2 column array with column 1 = an integer and column 2 = chars, and after populating the array, sort on column 2 with column 1 sorted relatively. Thanks! Dinesh ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] array of different datatypes
Dinesh B Vadhia wrote: I have looked (honestly!) and cannot see an array structure to allow different datatypes per column. I need a 2 column array with column 1 = an integer and column 2 = chars, and after populating the array, sort on column 2 with column 1 sorted relatively. If by "array" you mean a regular Python list, the data type of every single element may be different. So it's just how lists always work. a = [[1, 'hello'], [2, 'world'], [42, 'fish'], ] Thanks! Dinesh ___ 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] array of different datatypes
Thanks Steve. How do you sort on the second element of each list to get: a' = [[42, 'fish'], [1, 'hello'] [2, 'world'] ] From: Steve Willoughby Sent: Monday, September 22, 2008 8:16 PM To: Dinesh B Vadhia Cc: tutor@python.org Subject: Re: [Tutor] array of different datatypes Dinesh B Vadhia wrote: > I have looked (honestly!) and cannot see an array structure to allow > different datatypes per column. I need a 2 column array with column 1 = an > integer and column 2 = chars, and after populating the array, sort on column > 2 with column 1 sorted relatively. If by "array" you mean a regular Python list, the data type of every single element may be different. So it's just how lists always work. a = [[1, 'hello'], [2, 'world'], [42, 'fish'], ] > Thanks! > > Dinesh > > > > > > ___ > 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] array of different datatypes
Dinesh B Vadhia wrote: Thanks Steve. How do you sort on the second element of each list to get: a' = [[42, 'fish'], [1, 'hello'] [2, 'world'] ] something like this would do the trick: a_prime = sorted(a, key=(lambda i: i[1])) sorted(a) returns a new list consisting of the elements in a but in sorted order. the key= parameter says how to derive the sort key from any given element; in this case, the elements being sorted are themselves lists, and element #1 in the sub-list (a.k.a. "row") is the key. From: Steve Willoughby Sent: Monday, September 22, 2008 8:16 PM To: Dinesh B Vadhia Cc: tutor@python.org Subject: Re: [Tutor] array of different datatypes Dinesh B Vadhia wrote: I have looked (honestly!) and cannot see an array structure to allow different datatypes per column. I need a 2 column array with column 1 = an integer and column 2 = chars, and after populating the array, sort on column 2 with column 1 sorted relatively. If by "array" you mean a regular Python list, the data type of every single element may be different. So it's just how lists always work. a = [[1, 'hello'], [2, 'world'], [42, 'fish'], ] Thanks! Dinesh ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor