Two Plus Two Poker Forums

Two Plus Two Poker Forums (https://forumserver.twoplustwo.com/)
-   Computer and Technical Help (https://forumserver.twoplustwo.com/48/computer-technical-help/)
-   -   ** Python Support Thread ** (https://forumserver.twoplustwo.com/48/computer-technical-help/python-support-thread-1007515/)

unluckyboy 06-23-2012 01:05 AM

Re: ** Python Support Thread **
 
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

unluckyboy 06-23-2012 01:08 AM

Re: ** Python Support Thread **
 
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

Alex Wice 06-23-2012 05:10 AM

Re: ** Python Support Thread **
 
Quote:

Originally Posted by unluckyboy (Post 33365426)
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)


fluorescenthippo 06-30-2012 12:24 PM

Re: ** Python Support Thread **
 
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

Xhad 06-30-2012 12:45 PM

Re: ** Python Support Thread **
 
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

fluorescenthippo 07-01-2012 07:16 PM

Re: ** Python Support Thread **
 
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.

Xhad 07-02-2012 04:22 AM

Re: ** Python Support Thread **
 
How much do you know about classes? Those are for operator overloading ('<' and '==', respectively).

pokergrader 07-07-2012 04:48 PM

Re: ** Python Support Thread **
 
Quote:

Originally Posted by Xhad (Post 33534306)
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


clowntable 07-07-2012 05:41 PM

Re: ** Python Support Thread **
 
Same benchmark with PyPy please :)

pokergrader 07-07-2012 05:55 PM

Re: ** Python Support Thread **
 
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.

kerowo 07-07-2012 06:26 PM

Re: ** Python Support Thread **
 
Code:

Manual copy: 13.7768809795
List comprehension: 3.7635409832

Woot! Retina Display MBP is quick!

clowntable 07-08-2012 08:37 AM

Re: ** Python Support Thread **
 
List comprehensions that aren't super complicated are a lot easier to read than loops as well imo

daveT 07-08-2012 08:26 PM

Re: ** Python Support Thread **
 
Quote:

Originally Posted by kerowo (Post 33643509)
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

kerowo 07-08-2012 08:32 PM

Re: ** Python Support Thread **
 
What's that got, a 5x5 resolution screen... :)

Mariogs37 07-11-2012 04:05 PM

Re: ** Python Support Thread **
 
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?

Xhad 07-11-2012 04:28 PM

Re: ** Python Support Thread **
 
are you actually capitalizing if and elif? that isnt' allowed

Mariogs37 07-11-2012 05:27 PM

Re: ** Python Support Thread **
 
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.

Neko 07-11-2012 06:43 PM

Quote:

Originally Posted by Mariogs37 (Post 33705566)
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


Neko 07-11-2012 08:16 PM

Re: ** Python Support Thread **
 
Quote:

Originally Posted by Neko (Post 33706614)
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 :/

Mariogs37 07-11-2012 11:01 PM

Re: ** Python Support Thread **
 
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.

fluorescenthippo 07-12-2012 08:59 PM

Re: ** Python Support Thread **
 
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...

tyler_cracker 07-12-2012 09:06 PM

Re: ** Python Support Thread **
 
Unittest
Numpy
Scipy
Django
Functools(!)
Tornado

Good luck!

Neko 07-12-2012 09:29 PM

Re: ** Python Support Thread **
 
Quote:

Originally Posted by Mariogs37 (Post 33710663)
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.

Mariogs37 07-13-2012 06:12 PM

Re: ** Python Support Thread **
 
Thanks a lot, Neko.

tyler_cracker 07-13-2012 07:19 PM

Re: ** Python Support Thread **
 
Quote:

Originally Posted by tyler_cracker (Post 33727503)
Unittest
Numpy
Scipy
Django
Functools(!)
Tornado

Good luck!

hippo,

done with these yet? i can suggest more when you're ready! ;)


All times are GMT -4. The time now is 03:50 PM.

Powered by vBulletin®
Copyright ©2000 - 2026, Jelsoft Enterprises Ltd.

Copyright © 2008-2020, Two Plus Two Interactive