Open Side Menu Go to the Top
Register
** Python Support Thread ** ** Python Support Thread **

06-23-2012 , 01:05 AM
i am trying tree.xpath("//{http://odds.sbrforum.com}side-name"), and root.xpath("//{http://odds.sbrforum.com}side-name"), tree.xpath("//{http://odds.sbrforum.com}:side-name"), and root.xpath("//side-name")

none work
** Python Support Thread ** Quote
06-23-2012 , 01:08 AM
when i try to grab some random element and do tree.getpath(random_element) it gives me bull**** paths like /*/*[1]

what the hell is that
** Python Support Thread ** Quote
06-23-2012 , 05:10 AM
Quote:
Originally Posted by unluckyboy
here is a programming problem i am having a problem with. i have data for 1300 football games. for each game i have the spread, and what the result was compared to the spread. it is set up as a list of lists now [(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 two pieces of data:

spread: game1_result_compared_to_spread==0/total_games_with_this_spread
spread2: game1_result_compared_to_spread==0/total_games_with_this_spread
etc. for each spread

how can i get this?
I might not have understood your question btw.
Code:
def avgspread(sp,lt):
   #given a spread, and a list of games [ (spread,result),... ]
   # , return the average result compared to spread
   ans,ct = 0,0
   for i in lt:
       if i[0] == sp:
         ans += i[1]
         ct += 1
   return ans/float(ct)
** Python Support Thread ** Quote
06-30-2012 , 12:24 PM
taking the MIT python course and came across this:

self.viruses = [x for x in newViruses]


can someone please explain to me how this is better than a typical for loop. I'm also uncertain as to what this does
** Python Support Thread ** Quote
06-30-2012 , 12:45 PM
That's called a list comprehension. In this case it's just copying the list. The equivalent as a for would be:

Code:
self.viruses = []
for x in newViruses:
    self.viruses.append(x)
The main advantage is code length and readability once you understand it; see my response to this post
** Python Support Thread ** Quote
07-01-2012 , 07:16 PM
what do __lt__ and __eq__ do? I keep getting examples when i google but not an explanation. basically im confused as to how a class can return more than one thing using these fuctions.
** Python Support Thread ** Quote
07-02-2012 , 04:22 AM
How much do you know about classes? Those are for operator overloading ('<' and '==', respectively).
** Python Support Thread ** Quote
07-07-2012 , 04:48 PM
Quote:
Originally Posted by Xhad
That's called a list comprehension. In this case it's just copying the list. The equivalent as a for would be:

Code:
self.viruses = []
for x in newViruses:
    self.viruses.append(x)
The main advantage is code length and readability once you understand it; see my response to this post
List comprehensions are also faster (and in some cases, much faster). Python is a slow language, and when you write out a for loop python actually has to continually run over the loop to execute it, which is slow. List comprehensions, since they encapsulate the operation you are trying to do more comprehensively, allow python to optimize the execution.

For example in the below code, I'm manually copying a list with 1000 items and then doing the same operation with a list comprehension. The list comprehension is about 2.5x faster to do the exact same operation.

Code:
import timeit

things = [0] * 1000

def manual_copy():
    copy = []
    for thing in things:
        copy.append(thing)

def list_comprehension():
    copy = [thing for thing in things]

t = timeit.Timer('manual_copy()', 'from __main__ import manual_copy')
print 'Manual copy:', t.timeit(number=100000)

t = timeit.Timer(
    'list_comprehension()', 'from __main__ import list_comprehension')
print 'List comprehension:', t.timeit(number=100000)
Output (time in seconds, lower is faster):
Code:
Manual copy: 16.9621839523
List comprehension: 6.79605603218
** Python Support Thread ** Quote
07-07-2012 , 05:41 PM
Same benchmark with PyPy please
** Python Support Thread ** Quote
07-07-2012 , 05:55 PM
Same code in pypy 1.9

Output in seconds, lower is better:
Code:
Manual copy: 5.08202004433
List comprehension: 1.11676502228
It is just much easier for the python interpreter to optimize a list comprehension than it is to optimize a for loop, regardless of which one you are using

Of course one of the reasons for this is list comprehensions are much less flexible than for loops. But the general rule is that if you are doing something simple that can be a list comprehension, it should be a list comprehension. However, if you try to cram something too complicated into a list comprehension it can get really gnarly and unreadable, and at that point you should probably just use a for loop for readability. There was a python style rule where I used to work that if your list comprehension was longer than 2 lines (80 character width), then it was too complicated and needed to be refactored somehow.

Last edited by pokergrader; 07-07-2012 at 06:01 PM.
** Python Support Thread ** Quote
07-07-2012 , 06:26 PM
Code:
Manual copy: 13.7768809795
List comprehension: 3.7635409832
Woot! Retina Display MBP is quick!
** Python Support Thread ** Quote
07-08-2012 , 08:37 AM
List comprehensions that aren't super complicated are a lot easier to read than loops as well imo
** Python Support Thread ** Quote
07-08-2012 , 08:26 PM
Quote:
Originally Posted by kerowo
Code:
Manual copy: 13.7768809795
List comprehension: 3.7635409832
Woot! Retina Display MBP is quick!
$530 i5 Lenovo:

Manual copy: 10.962864498256714
List comprehension: 3.4187096686791882

Last edited by daveT; 07-08-2012 at 08:27 PM. Reason: yeah, trolling ftw!
** Python Support Thread ** Quote
07-08-2012 , 08:32 PM
What's that got, a 5x5 resolution screen...
** Python Support Thread ** Quote
07-11-2012 , 04:05 PM
So I'm getting a syntax error here for month in the third line...not sure why:

month = int(input("Please give me an integer for the month: "))

day = int(input("Please give me an integer for the day of the month: "))


# Now we test that the input date is during the 2011-2012 academic year and
# is valid (e.g. 1/37 is not a valid date).

If month == 9 and day == 4:
print("This date is not during the academic year")

Elif month == 1 or month == 3 or month == 5 or month == 7 or month == 8 or month == 10 or month == 12 and day < 1 or day > 31
print("This date is invalid")

Elif month == 4 or month == 6 or month == 9 or month == 11 and day < 1 or day > 30
print("This date is invalid")

Anyone know why?
** Python Support Thread ** Quote
07-11-2012 , 04:28 PM
are you actually capitalizing if and elif? that isnt' allowed
** Python Support Thread ** Quote
07-11-2012 , 05:27 PM
Ah, I'm ******ed. Also, is there an efficient way to do this:

Elif month == 1 or month == 3 or month == 5 or month == 7 or month == 8 or month == 10 or month == 12 and day < 1 or day > 31
print("This date is invalid")

What I'm trying to get it to do is:

If month == 1 and day < 1 OR month ==1 and day > 31 OR month == 3 and day < 1...

Know what I mean?

Thanks again for the help.
** Python Support Thread ** Quote
07-11-2012 , 06:43 PM
Quote:
Originally Posted by Mariogs37
Ah, I'm ******ed. Also, is there an efficient way to do this:

Elif month == 1 or month == 3 or month == 5 or month == 7 or month == 8 or month == 10 or month == 12 and day < 1 or day > 31
print("This date is invalid")

What I'm trying to get it to do is:

If month == 1 and day < 1 OR month ==1 and day > 31 OR month == 3 and day < 1...

Know what I mean?

Thanks again for the help.
Code:
If month in (3, 5, 7,) and (day <	1 or 31 <	day):
				foo
** Python Support Thread ** Quote
07-11-2012 , 08:16 PM
Quote:
Originally Posted by Neko
Code:
If month in (3, 5, 7,) and (day < 1 or 31 < day):
    foo
Sent that from my phone not sure why it inserted tabs instead of spaces :/
** Python Support Thread ** Quote
07-11-2012 , 11:01 PM
perfect, thanks a lot man. one last thing: is there a good place to go over these exercises? my book only has super basic ones...

thanks again.
** Python Support Thread ** Quote
07-12-2012 , 08:59 PM
So I'm getting a good feel for python now. I would still like to be more familiar with all the popular packages and modules. What should I be getting familiar with? sys,os,itertools,etc...
** Python Support Thread ** Quote
07-12-2012 , 09:06 PM
Unittest
Numpy
Scipy
Django
Functools(!)
Tornado

Good luck!
** Python Support Thread ** Quote
07-12-2012 , 09:29 PM
Quote:
Originally Posted by Mariogs37
perfect, thanks a lot man. one last thing: is there a good place to go over these exercises? my book only has super basic ones...

thanks again.
Try Project Euler. It can be pretty fun if you like math and there are tonnes of solutions available online to help when you get stuck.
** Python Support Thread ** Quote
07-13-2012 , 06:12 PM
Thanks a lot, Neko.
** Python Support Thread ** Quote
07-13-2012 , 07:19 PM
Quote:
Originally Posted by tyler_cracker
Unittest
Numpy
Scipy
Django
Functools(!)
Tornado

Good luck!
hippo,

done with these yet? i can suggest more when you're ready!
** Python Support Thread ** Quote

      
m