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/)

Madmax2000 10-28-2011 11:02 AM

Re: ** Python Support Thread **
 
Quote:

Originally Posted by TheIrishThug (Post 29522283)
Can we see the code you ran and the actual error message? This will make it easier for us to see what is actually going wrong and why it is happening.

http://i1090.photobucket.com/albums/...ythonerror.jpg

I wrote the code in python command line, then saved it as a .py, then ran it through IDLE, I clicked run --> check module-- then I get the syntax error.

TheIrishThug 10-28-2011 11:29 AM

Re: ** Python Support Thread **
 
Ok, there is the problem. Saving the text from the command line includes text that is not part of the code that runs.

Delete any line that doesn't have ">>>" before it. Then delete all of the instances of ">>>". You should end up with something very close to what Gazillion posted. That is the actual Python code. Everything else is there as part of the interactive python shell.

Gazillion 10-28-2011 12:47 PM

Re: ** Python Support Thread **
 
Lol, yes - very much what Irishthug said :)

It would be a really good idea to not get into the habit of copying the output of the command line into a text file and saving it as a .py

Think of them as two seperate entities. Just use the command line if you want to quickly experiment with new commands etc. For actually writing a piece of code that you wish to execute at will, always do it from a blank file in a text editor or IDLE and save it as a .py

The command line will have a tonne of stuff that is specific to it and it alone, including all the stuff that you've already discovered to be problematic.

m_hags 10-31-2011 05:34 AM

Re: ** Python Support Thread **
 
madmax, I'm just learning now to program and am working through the tutorial here: http://learnpythonthehardway.org/ the book is free if you view it online. what you're doing made me cringe. I think you are a step or two ahead of yourself and should maybe check this out.

daveT 10-31-2011 11:00 PM

Re: ** Python Support Thread **
 
That MIT class ~is~ the hard way to learn Python. :p I haven't read the code in that book but the author is pretty entertaining.

m_hags 11-01-2011 01:23 AM

Re: ** Python Support Thread **
 
what course number are you referring to? I found it earlier, but it looked like I missed the start date or something and didn't investigate past that. Now I can't find the link.

calm 11-03-2011 07:30 PM

Re: ** Python Support Thread **
 
Pretty cool trick I just learned, using an else statement with a for loop...

Instead of:
Code:

found = False
for count in xrange(10):
    if count == number_wanted:
            # do something
            found = True
            break
if not found:
    # do something else

It's easier and cleaner like this:
Code:

for count in xrange(10):
    if count == number_wanted:
            # do something
            break
else:
    # do something else


RoundTower 11-03-2011 08:00 PM

Re: ** Python Support Thread **
 
I always mix up how that works, I can't decide if I should claim it's bad style or just get more used to it.

TheIrishThug 11-03-2011 08:36 PM

Re: ** Python Support Thread **
 
If more languages supported it, it would be fine. The fact that most people don't know what it means, is what causes the issue.

daveT 11-05-2011 03:58 PM

Re: ** Python Support Thread **
 
Quote:

Originally Posted by calm (Post 29637313)
Pretty cool trick I just learned, using an else statement with a for loop...

Instead of:
Code:

found = False
for count in xrange(10):
    if count == number_wanted:
            # do something
            found = True
            break
if not found:
    # do something else

It's easier and cleaner like this:
Code:

for count in xrange(10):
    if count == number_wanted:
            # do something
            break
else:
    # do something else


The second option looks best to me. Not sure how I feel about the first version. I think it was probably done that way because whoever is teaching you the language is trying to make things explicit, but not sure if that is a good thing or not, tbh. Sort of like using 'try:' for every single variable your function takes.

Just an interesting tidbit about xrange():

xrange is syntatically replaced by range() in Python3 so that range() has the same function as xrange().

range is (in many cases) supposed to be written as list(range()).

calm 11-05-2011 04:57 PM

Re: ** Python Support Thread **
 
Quote:

Originally Posted by daveT (Post 29666034)
The second option looks best to me. Not sure how I feel about the first version. I think it was probably done that way because whoever is teaching you the language is trying to make things explicit, but not sure if that is a good thing or not, tbh. Sort of like using 'try:' for every single variable your function takes.

Just an interesting tidbit about xrange():

xrange is syntatically replaced by range() in Python3 so that range() has the same function as xrange().

range is (in many cases) supposed to be written as list(range()).

The first version is just an example of how I used to write that loop before learning the else case existed.

I'm still using Python 2.6, and there xrange() is more efficient memory-wise than range(). It always confused me though, so I'm glad to hear in 3.x that the two get consolidated.

Xhad 11-05-2011 09:02 PM

Re: ** Python Support Thread **
 
I love the loop-else construct so much that I use it even when it's not necessary (because the "break" was actually a function return statement). That way I find it easier to remember what I did for some reason.

daveT 11-05-2011 10:41 PM

Re: ** Python Support Thread **
 
Quote:

Originally Posted by calm (Post 29666965)
The first version is just an example of how I used to write that loop before learning the else case existed.

I'm still using Python 2.6, and there xrange() is more efficient memory-wise than range(). It always confused me though, so I'm glad to hear in 3.x that the two get consolidated.

Python3 doesn't actually consolidate them.

range() works by turning the iterator into a list object first and then computing, and considering that many programmers don't know about xrange(), this only serves to slow down the programs written with python. xrange() simply counts out the iterators and computes on it one after another.

So, unless you really need a list of values, range() isn't very useful. What Python3 did is default range to act like xrange(). So now if you want your iterators to become lists (make range() act like the old range()), you have to make it explicit that you do.

list(range())

Of course, this opens up a whole can of worms about the "best" way to do things, but for the most part, the difference in range() in 2 and 3 is superficial and I don't think most programmers moving from 2 to 3 will ever know the difference. I suspect that this was done on purpose by the Python overlords.

Xhad 11-07-2011 05:25 PM

Re: ** Python Support Thread **
 
Quote:

Originally Posted by daveT (Post 28637452)
Code:

#this no longer works

def myFun(a, b):
    one = a
    two = b
    myVar = one, two
    return myVar


So, I actually use Python 3 exclusively and this has been bugging me for awhile. In what sense does this function "not work"? It looks and runs fine to me.

Also, in response to a different discussion, either they didn't remove reduce from Python3 or they changed their minds: http://docs.python.org/release/3.1.3...nctools.reduce

daveT 11-09-2011 09:43 PM

Re: ** Python Support Thread **
 
Quote:

Originally Posted by Xhad (Post 29707403)
So, I actually use Python 3 exclusively and this has been bugging me for awhile. In what sense does this function "not work"? It looks and runs fine to me.

Can't really remember what it was I was thinking about when I wrote that. Maybe there is something different in the output or there was something wrong with the original install I had. I did reinstall 3.

Sorry, I'm already forgetting 2.

Xhad 11-12-2011 10:46 PM

Re: ** Python Support Thread **
 
np, I was just puzzled more than anything

btw, in case you're still using int() to fix division results, a shorter way to get around the integer division problem is to use // (integer division) instead of / (division). So, this:

Code:

x = 10//2

for i in range(x):
    print(i, end=' ')

will print "0 1 2 3 4"

daveT 11-13-2011 02:22 AM

Re: ** Python Support Thread **
 
Quote:

Originally Posted by Xhad (Post 29801647)
np, I was just puzzled more than anything

btw, in case you're still using int() to fix division results, a shorter way to get around the integer division problem is to use // (integer division) instead of / (division). So, this:

Code:

x = 10//2

for i in range(x):
    print(i, end=' ')

will print "0 1 2 3 4"

Nice one. I didn't know this. ty.

calm 11-13-2011 02:53 PM

Re: ** Python Support Thread **
 
I've been programming in Python for a few years now and never learned list comprehensions. They seem to pop up a lot in other people's code, but so far I've never had a problem getting things done other ways. What are their benefits over just a standard for-loop to process data? Are there any times where I *really* should be using list comprehensions?

Edit: Map and filter too, I've never used those. Worth learning?

Neko 11-13-2011 03:50 PM

Re: ** Python Support Thread **
 
You should be using them anywhere that their use makes your code shorter/more concise without sacrificing clarity of purpose/readability.

I'd rather see:

Code:

rands = [random.random() for x in range(10000)]
then

Code:

rands = []
for x in range(10000):
    rands.append(random.random())

The former reduces the code from 3 lines to 1, without sacrificing any clarity. The first example will also run faster then the second code which is another benefit.


I'd often use map for something like:

Code:

some_string = "1,2,3,4"
new_list = map(int,some_string.split(','))

But this could also be achieved with a list comprehension:


Code:

some_string = "1,2,3,4"
new_list = [int(x) for x in some_string.split(',')]

With filter I often find a list comprehension is a little nicer to look at:

Code:

odds = [x for x in range(10) if x%2 != 0]
vs:

Code:

odds = filter(lambda x: x%2!=0,range(10))
That said, map, filter & reduce are all worth learning about even if you don't use them too often!

calm 11-13-2011 04:02 PM

Re: ** Python Support Thread **
 
Thanks.

So looking at my code, this seems like a good candidate...

Code:

header = ['Column A','Column B','Column C']
for book in book_list:
    if book in settings['books']:
        header.append(book)

I've translated the loop to this list comprehension:
Code:

header = [book for book in book_list if book in settings['book']]
but how do i make sure Column A, B and C get put at the front of the list (before the books)?

Code:

header = ['Column A','Column B','Column C'].extend([book for book in book_list if book in settings['book']])
maybe?

Neko 11-13-2011 04:27 PM

Re: ** Python Support Thread **
 
Quote:

Originally Posted by calm (Post 29811248)
Thanks.

So looking at my code, this seems like a good candidate...

Code:

header = ['Column A','Column B','Column C']
for book in book_list:
    if book in settings['books']:
        header.append(book)

I've translated the loop to this list comprehension:
Code:

header = [book for book in book_list if book in settings['book']]
but how do i make sure Column A, B and C get put at the front of the list (before the books)?

You could do either:

Code:

header = ['Column A','Column B','Column C']  + [book for book in book_list if book in settings['book']]
or


Code:

header =  [book for book in book_list if book in settings['book']]
header = ['Column A','Column B','Column C']  + header

or


Code:

header = ['Column A','Column B','Column C']
header = header.extend([book for book in book_list if book in settings['book']])


daveT 11-13-2011 04:38 PM

Re: ** Python Support Thread **
 
Quote:

Originally Posted by calm (Post 29811248)
Thanks.

So looking at my code, this seems like a good candidate...

Code:

header = ['Column A','Column B','Column C']
for book in book_list:
    if book in settings['books']:
        header.append(book)

I've translated the loop to this list comprehension:
Code:

header = [book for book in book_list if book in settings['book']]
but how do i make sure Column A, B and C get put at the front of the list (before the books)?

Code:

header = ['Column A','Column B','Column C'].extend([book for book in book_list if book in settings['book']])
maybe?

What do your output to look like?

Are you saying you want:

[columnA [bookA, bookB], columnB....]

or

[columnA, columnB, columnC, bookA, bookB, bookC...]

Because these are two very different questions, but if you want option 2, you shouldn't have to do anything. List are ordered objects, so if you put 1 in, then you put it in spot 1.

Code:

>>> a = []
>>> for i in range(1, 10):
        a.append(i)
        print(a)

       
[1]
[1, 2]
[1, 2, 3]
[1, 2, 3, 4]
[1, 2, 3, 4, 5]
[1, 2, 3, 4, 5, 6]
[1, 2, 3, 4, 5, 6, 7]
[1, 2, 3, 4, 5, 6, 7, 8]
[1, 2, 3, 4, 5, 6, 7, 8, 9]
>>>


calm 11-13-2011 04:53 PM

Re: ** Python Support Thread **
 
Yes I want option 2, but as a list comprehension instead of a for loop. Think I got it figured out thanks to Neko.

Here's another for loop I'm trying to substitute with a list comprehension:
Code:

row_data = []
row_color = []
for key in dict:
    row_data.append([])
    row_color.append([])

Now I could easily do this with two separate list comprehensions:
Code:

row_data = [[] for key in dict]
row_color = [[] for key in dict]

But since I'm then looping over the dict twice, I guess the for loop would be faster? Is there a way to combine the two in one list comprehension?

daveT 11-13-2011 04:58 PM

Re: ** Python Support Thread **
 
But he didn't tell you which one is the 'Pythonic' way. :D

calm 11-13-2011 05:12 PM

Re: ** Python Support Thread **
 
Code:

row_data,row_color = [[[] for key in dict] for x in xrange(2)]
Any better?


All times are GMT -4. The time now is 04:26 PM.

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

Copyright © 2008-2020, Two Plus Two Interactive