Re: Beginner help
On Sunday, February 5, 2017 at 11:27:19 PM UTC, pha...@u.rochester.edu wrote: > Hi everyone. I'm new to python and have hit a bit of a wall with an > assignment I'm working on. I created a number of classes and instantiated > them, now I need to create a list out of them. I am looking for something > more elegant than appending each object to the list as I instantiate it. I > tried setting up an if loop, but didn't get far with it. Any suggestions? If loop, for heavens sake :) Try one of these beasties instead https://docs.python.org/3/tutorial/datastructures.html#list-comprehensions Kindest regards. Mark Lawrence. -- https://mail.python.org/mailman/listinfo/python-list
Beginner help
Hi everyone. I'm new to python and have hit a bit of a wall with an assignment I'm working on. I created a number of classes and instantiated them, now I need to create a list out of them. I am looking for something more elegant than appending each object to the list as I instantiate it. I tried setting up an if loop, but didn't get far with it. Any suggestions? -- https://mail.python.org/mailman/listinfo/python-list
adding values from a csv column and getting the mean. beginner help
I know the problem is with the for loop but don't know how to fix. Any help with explanation would be appreciated. #!/bin/env python import csv import sys if len(sys.argv) 3: print('Please specify a filename and column number: {} [csvfile] [column]'.format(sys.argv[0])) sys.exit(1) filename = sys.argv[1] column = int(sys.argv[2]) for line in filename() , column (): elements = line.strip().split(',') values.append(int(elements[col])) csum = sum(values) cavg = sum(values)/len(values) print(Sum of column %d: %f % (col, csum)) print(Avg of column %d: %f % (col, cavg)) -- https://mail.python.org/mailman/listinfo/python-list
Re: adding values from a csv column and getting the mean. beginner help
On 2013-12-11 11:10, brian cleere wrote: filename = sys.argv[1] column = int(sys.argv[2]) for line in filename() , column (): elements = line.strip().split(',') values.append(int(elements[col])) 1) you need to open the file 2) you need to make use of the csv module on that file 3) you need to extract the column Thus it would looks something like column = int(sys.argv[2]) f = open(sys.argv[1], rb) r = csv.reader(f) try: for row in r: values.append(int(row[column])) finally: f.close() which can be obtusely written as values = [int(row[column]) for row in csv.reader(open(sys.argv[1], rb))] though the more expanded version allows you to do better error handling (rows with insufficient columns, non-numeric/non-integer values in the specified column, etc). -tkc -- https://mail.python.org/mailman/listinfo/python-list
Re: adding values from a csv column and getting the mean. beginner help
On Thu, Dec 12, 2013 at 6:10 AM, brian cleere briancle...@gmail.com wrote: I know the problem is with the for loop but don't know how to fix. Any help with explanation would be appreciated. Your problem is akin to debugging an empty file :) It's not so much a matter of fixing what's not working as of starting at the very beginning: How do you iterate over the content of a CSV file? Now, you're almost there... partly. You have the split() call, which will split on the comma, so if you go that route, all you need to do is open the file, using the aptly-named builtin function open. You'll find docs on that if you do a quick search. But you're actually part-way to the better solution. You're importing the 'csv' module, which is exactly what you need here. All you need is to read up on its docs: http://docs.python.org/3/library/csv.html I'm sure you can figure out the rest of your homework from there! Now, with that out of the way, I'd like to just mention a couple of other things. print('Please specify a filename and column number: {} [csvfile] [column]'.format(sys.argv[0])) Square brackets in a usage description often mean optional. You may want to be careful of that. There's no really good solution though. csum = sum(values) cavg = sum(values)/len(values) Once you've calculated the sum once, you can reuse that to calculate the average. Can you see how? :) And finally: You're using Google Groups to post, which means your paragraphs are unwrapped, and - unless you fight very hard against a stupidly buggy piece of software - your replies will be malformed and ugly. Don't make yourself look bad; switch to a better newsreader, or to the mailing list: https://mail.python.org/mailman/listinfo/python-list The content is the same, you just subscribe to the list and read and write as email. Thanks! And welcome to the group. ChrisA -- https://mail.python.org/mailman/listinfo/python-list
Re: adding values from a csv column and getting the mean. beginner help
On 11/12/2013 19:10, brian cleere wrote: I know the problem is with the for loop but don't know how to fix. Any help with explanation would be appreciated. #!/bin/env python import csv You never use the csv module. import sys if len(sys.argv) 3: print('Please specify a filename and column number: {} [csvfile] [column]'.format(sys.argv[0])) sys.exit(1) filename = sys.argv[1] column = int(sys.argv[2]) for line in filename() , column (): You're trying to loop around the filename and the column, you need to open the file and loop around that. elements = line.strip().split(',') Please don't do this when you've got the csv module to do things for you. values.append(int(elements[col])) Where did values come from? Is it col or column, please make your mind up? So let's stick things together. Something like. values = [] with open(filename) as csvfile: valuereader = csv.reader(csvfile) for row in valuereader: values.append(int(row[column])) csum = sum(values) cavg = sum(values)/len(values) print(Sum of column %d: %f % (col, csum)) print(Avg of column %d: %f % (col, cavg)) I like consistency, new style formatting here, old style above, still if it works for you. -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence -- https://mail.python.org/mailman/listinfo/python-list
Re: adding values from a csv column and getting the mean. beginner help
On 11/12/2013 19:22, Chris Angelico wrote: On Thu, Dec 12, 2013 at 6:10 AM, brian cleere briancle...@gmail.com wrote: I know the problem is with the for loop but don't know how to fix. Any help with explanation would be appreciated. Your problem is akin to debugging an empty file :) It's not so much a matter of fixing what's not working as of starting at the very beginning: How do you iterate over the content of a CSV file? Now, you're almost there... partly. You have the split() call, which will split on the comma, so if you go that route, all you need to do is open the file, using the aptly-named builtin function open. You'll find docs on that if you do a quick search. But you're actually part-way to the better solution. You're importing the 'csv' module, which is exactly what you need here. All you need is to read up on its docs: http://docs.python.org/3/library/csv.html I'm sure you can figure out the rest of your homework from there! Now, with that out of the way, I'd like to just mention a couple of other things. print('Please specify a filename and column number: {} [csvfile] [column]'.format(sys.argv[0])) Square brackets in a usage description often mean optional. You may want to be careful of that. There's no really good solution though. There is, https://pypi.python.org/pypi/docopt/0.6.1 :) csum = sum(values) cavg = sum(values)/len(values) Once you've calculated the sum once, you can reuse that to calculate the average. Can you see how? :) And finally: You're using Google Groups to post, which means your paragraphs are unwrapped, and - unless you fight very hard against a stupidly buggy piece of software - your replies will be malformed and ugly. Don't make yourself look bad; switch to a better newsreader, or to the mailing list: https://mail.python.org/mailman/listinfo/python-list The content is the same, you just subscribe to the list and read and write as email. Ooh 'eck, we'll have the Popular Front for the Liberation of Google Groups squad out in force again, vainly trying to defend the bug ridden crap that they insist on using, and which I obviously won't mention. Whoops!!! Oh Lord, won't you buy me Mozilla Thunderbird ? My friends all use GG, I think that's absurd. Worked hard all my lifetime, no help from the nerds, So Lord, won't you buy me Mozilla Thunderbird ? With apologies to the late, great Janis Joplin. Thanks! And welcome to the group. ChrisA -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence -- https://mail.python.org/mailman/listinfo/python-list
Re: adding values from a csv column and getting the mean. beginner help
On Thu, Dec 12, 2013 at 6:41 AM, Mark Lawrence breamore...@yahoo.co.uk wrote: Square brackets in a usage description often mean optional. You may want to be careful of that. There's no really good solution though. There is, https://pypi.python.org/pypi/docopt/0.6.1 :) That appears to use x for a mandatory argument x, which is then slightly ambiguous with shell redirection. But that's the best notation I've ever seen for distinguishing mandatory args from fixed keywords. CrisA -- https://mail.python.org/mailman/listinfo/python-list
Re: adding values from a csv column and getting the mean. beginner help
On 11/12/2013 19:46, Chris Angelico wrote: On Thu, Dec 12, 2013 at 6:41 AM, Mark Lawrence breamore...@yahoo.co.uk wrote: Square brackets in a usage description often mean optional. You may want to be careful of that. There's no really good solution though. There is, https://pypi.python.org/pypi/docopt/0.6.1 :) That appears to use x for a mandatory argument x, which is then slightly ambiguous with shell redirection. But that's the best notation I've ever seen for distinguishing mandatory args from fixed keywords. CrisA I use the alternative X for a mandatory argument X. -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence -- https://mail.python.org/mailman/listinfo/python-list
Re: adding values from a csv column and getting the mean. beginner help
On Thu, Dec 12, 2013 at 7:00 AM, Mark Lawrence breamore...@yahoo.co.uk wrote: I use the alternative X for a mandatory argument X. Also common, but how do you specify a keyword, then? Say you have a command with subcommands: $0 foo x y Move the foo to (x,y) $0 bar x y z Go to bar X, order a Y, and Z it [eg 'compress', 'gzip', 'drink'] How do you show that x/y/z are mandatory args, but foo/bar are keywords to be typed exactly? In some formats italicized text can make that distinction, but not in pure text. ChrisA -- https://mail.python.org/mailman/listinfo/python-list
Re: adding values from a csv column and getting the mean. beginner help
On 2013-12-12 07:03, Chris Angelico wrote: Also common, but how do you specify a keyword, then? Say you have a command with subcommands: $0 foo x y Move the foo to (x,y) $0 bar x y z Go to bar X, order a Y, and Z it [eg 'compress', 'gzip', 'drink'] How do you show that x/y/z are mandatory args, but foo/bar are keywords to be typed exactly? In some formats italicized text can make that distinction, but not in pure text. I prefer {} notation: $0 mv [--optional] {x} {y} $0 bar [--mutually|--exclusive] {x} {y} {z} -tkc -- https://mail.python.org/mailman/listinfo/python-list
Re: adding values from a csv column and getting the mean. beginner help
On 11/12/2013 20:03, Chris Angelico wrote: On Thu, Dec 12, 2013 at 7:00 AM, Mark Lawrence breamore...@yahoo.co.uk wrote: I use the alternative X for a mandatory argument X. Also common, but how do you specify a keyword, then? Say you have a command with subcommands: $0 foo x y Move the foo to (x,y) $0 bar x y z Go to bar X, order a Y, and Z it [eg 'compress', 'gzip', 'drink'] How do you show that x/y/z are mandatory args, but foo/bar are keywords to be typed exactly? In some formats italicized text can make that distinction, but not in pure text. ChrisA Haven't a clue off the top of my head so read all about it here https://github.com/docopt/docopt -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence -- https://mail.python.org/mailman/listinfo/python-list
Re: adding values from a csv column and getting the mean. beginner help
On 12/11/2013 01:41 PM, Mark Lawrence wrote: On 11/12/2013 19:22, Chris Angelico wrote: There is, https://pypi.python.org/pypi/docopt/0.6.1 :) +1 for docopt. It makes everything very clear. Just type out your usage string, and then run docopt(usage_str) on it to get a dict of your args. When I saw the video at http://docopt.org my jaw dropped. I couldn't believe all of the arg parsing junk I had been writing for even the smallest scripts. The other arg parsing libs make it easier than manually doing it, but docopt is magic. -- - Christopher Welborn cjwelb...@live.com http://welbornprod.com -- https://mail.python.org/mailman/listinfo/python-list
Re: adding values from a csv column and getting the mean. beginner help
On 12/12/2013 03:10 AM, brian cleere wrote: I know the problem is with the for loop but don't know how to fix. Any help with explanation would be appreciated. #!/bin/env python import csv import sys if len(sys.argv) 3: print('Please specify a filename and column number: {} [csvfile] [column]'.format(sys.argv[0])) sys.exit(1) filename = sys.argv[1] column = int(sys.argv[2]) for line in filename() , column (): elements = line.strip().split(',') values.append(int(elements[col])) filename is a string, and column is an integer, the interpreter should warn you that they are not callable the above three lines could be changed to == values = [] for line in open(filename): elements = line.strip().split(',') values.append(int(elements[column])) or now that you have the csv module, you can use csv.reader to parse the file: == values = [ row[column] for row in csv.reader(open(filename, 'rb')) if len(row) column ] csum = sum(values) cavg = sum(values)/len(values) print(Sum of column %d: %f % (col, csum)) print(Avg of column %d: %f % (col, cavg)) -- https://mail.python.org/mailman/listinfo/python-list