![]() |
feedback on this python code
i'd like feedback on this python code i just did. i'd like to keep improving as a programmer.
i have data for 1300 football games. for each game i have the spread, and what the final score was compared to the spread. it is set up as a list of lists now all_line_differences=[(game1_spread, game1_result_compared_to_spread), (game2_spread, game2_result_compared_to_spread), etc.] i want to take this information and make a printout with these datas: spread, quantity_games, game1_result_compared_to_spread==0/total_games_with_this_spread spread2, quantity_games, game2_result_compared_to_spread==0/total_games_with_this_spread etc. for each spread THIS IS MY CODE ----------- PLEASE GIVE SUGGESTIONS ON HOW TO MAKE NICER OR MORE ELEGANT spread_quantity =dict() spread_tied = dict() for item in all_line_differences: spread_quantity[abs(item[0])]=0 spread_tied[abs(item[0])]=0 for item in all_line_differences: spread_quantity[abs(item[0])]+=1 if item[1] ==0: spread_tied[abs(item[0])]+=1 for key,value in sorted(spread_quantity.iteritems()): print key, " ", float(value), " ", spread_tied[key]/float(value) |
Re: feedback on this python code
i am guessing there is more elegant way to structure this task
|
Re: feedback on this python code
does this work as you want it to? If so I do not think there is much point in improving it. It is concise and relatively clear. You could make it more idiomatic, but that would achieve little.
|
Re: feedback on this python code
That might come across the wrong way, because I think writing idiomatic code is important in general, that you shouldn't always just write things the only way you know how. But I think you would understand that more if you wrote something longer and worse-written and realised the problems with it as you tried to extend it. This code simply isn't ugly enough.
having said that, here are some (arguable) improvements: Code:
spread_quantity =dict()Code:
for item in all_line_differences:Code:
for spread, diff in all_line_differences: |
Re: feedback on this python code
I just do a full select clause in the raw SQL and then something like this will unwrap the tuples:
Code:
a = ('''select query''')Just about every SQL language has the ability to add, subtract, divide, do abs(), etc, so you would only have to do some select query like: select game, result, up/down, abs(xyz) from table1, table2 where table1.game = table2.game or something along those lines. How complicated that query gets really depends on your schema and what it is you want to do. (Yes, I know the above query won't work at all) |
Re: feedback on this python code
Quote:
just worked through the defaultdict that is pretty cool. gonna work through your tuple suggestion now |
Re: feedback on this python code
okay figured out the tuple thing and also using the SQl query to get the required data from the first step. thanks for ideas.
|
Re: feedback on this python code
i have a math question related to this code. so as you can see what i am doing is finding the percentage of times a game pushes for each different spread. i get this nice printout...
1.0 147.0 0.0272108843537 1.5 25.0 0.0 2.0 18.0 0.0 2.5 67.0 0.0 3.0 264.0 0.0833333333333 etc... how do i find out the margin of error on the third column? |
Re: feedback on this python code
look while i try to figure out the margin of error on the last program which i am confused on for now i am starting my next task. finding out the correct spread for the 1st quarter based on 3 point spread for whole game. look applied learning:
def get_games(): return c.execute("select team1_line, team1_q1, team2_q2 from nfl_games where abs(team1_line)=3").fetchall() for one, two, three in get_games(): print one, two, three :) |
Re: feedback on this python code
use code tags
|
Re: feedback on this python code
I'll show you a program I use quite often that is similar to what you are doing, and try to explain the logic of the decisions. Of course, there is room for disagreement, but this may be helpful (of course, if I'm wrong anywhere, please feel free to correct me):
Code:
import postgresqlThe thing with your code is that you are allowing your functions to leak all over the place, which will wreak havoc now that you are attempting to extend it. The reason I say use SQL to create the list of tuples you want is for a few reasons: - SQL is built for data management. What would happen if you ran, say, two queries, and one list is shorter than the other? This would be interesting to program, but SQL does a perfect job of mashing and matching data. If you have SQL tuples with no information, that missing information issue will be confined to SQL, and won't create mis-matches in your program when you start combining and sorting lists. SQL also has the immensely helpful "order by" command which will automatically sort the tuples you need. If you have uneven lists and then you attempt to sort the lists in python followed by zipping them together without confirming the correct data matches, you are likely going to have a major headache and some wildly incorrect answers. - I only want to deal with well-defined data. In this case, I have a list that looks like: Code:
Code:
a = db.prepare('''select to_char(invoicedate, ' MM YYYY') as monthly, sum(qty) Code:
def createLines(x):Since I know how know the exact format that I am dealing with, I can easily create the following: Code:
a = db.prepare('''select to_char(invoicedate, ' MM YYYY') as monthly, sum(qty) Code:
for i in a(): |
Re: feedback on this python code
Wasnt sure if you accounted for a spread in the wrong direction.
For examples Bills favorite to win over Giants by 5. Giants win by 5. Should be off by 10 not 0 as abs might indicate. |
| All times are GMT -4. The time now is 04:40 AM. |
Powered by vBulletin®
Copyright ©2000 - 2026, Jelsoft Enterprises Ltd.
Copyright © 2008-2020, Two Plus Two Interactive