Two Plus Two Publishing LLC Two Plus Two Publishing LLC
 

Go Back   Two Plus Two Poker Forums > Other Topics > Programming

Notices

Programming Discussions about computer programming

Reply
 
Thread Tools Display Modes
Old 06-24-2011, 01:28 PM   #121
Carpal \'Tunnel
 
jjshabado's Avatar
 
Join Date: Jul 2006
Posts: 11,034
Re: ** Python Support Thread **

What's the best way for iterating over a list while filtering out values?

For example assume I have the following:

Code:
l = [1,None,3]
for x in l:
    if x:
         print 'Do a bunch of stuff'
So I can do:

Code:
l = [1,None,3]
for x in [y for y in l if y]:
    print 'Do a bunch of stuff'
But that still seems a bit wonky. I don't think there are any performance implications but it just doesn't look that great.

Any better ideas?
jjshabado is offline   Reply With Quote
Old 06-24-2011, 02:05 PM   #122
ɹǝʍoʇpunoɹ
 
RoundTower's Avatar
 
Join Date: Feb 2005
Location: soah made my profile
Posts: 13,925
Re: ** Python Support Thread **

first one looks fine to me.

You can also use
Code:
for x in filter(None, l):
RoundTower is offline   Reply With Quote
Old 06-24-2011, 02:09 PM   #123
ɹǝʍoʇpunoɹ
 
RoundTower's Avatar
 
Join Date: Feb 2005
Location: soah made my profile
Posts: 13,925
Re: ** Python Support Thread **

in Python 3 I understand you could use
Code:
[print(x) for x i l if x]
which returns a list of None, but has the correct side-effect of printing the non-False values. This won't work in Python 2 because print is not an object. You could wrap whatever you need to do inside the loop in a callable object though.
RoundTower is offline   Reply With Quote
Old 06-24-2011, 02:11 PM   #124
Carpal \'Tunnel
 
jjshabado's Avatar
 
Join Date: Jul 2006
Posts: 11,034
Re: ** Python Support Thread **

I was wondering how you'd do it without the use of filter (since some people are against that).

Your second one won't work for me since my print statement is just standing in for about half a dozen statements and I hate putting a lot of logic inside of a list comprehension.
jjshabado is offline   Reply With Quote
Old 06-24-2011, 07:01 PM   #125
S.A.G.E. Master
 
daveT's Avatar
 
Join Date: Jun 2005
Location: Why didn't I use Clojure instead?
Posts: 16,799
Re: ** Python Support Thread **

May seem totally out of place, but the only thing I am against is using 'l' and '1' in the same program. People like to use various fonts and some of those fonts aren't going to show the difference between 'l' and '1'. Even with a decent font, it is still easy to mistaken the stuff.
daveT is offline   Reply With Quote
Old 06-24-2011, 07:02 PM   #126
Carpal \'Tunnel
 
tyler_cracker's Avatar
 
Join Date: Apr 2005
Location: Shallow End OTKP
Posts: 13,905
Re: ** Python Support Thread **

lol shabby isn't this exactly the same as the list filtering problem i posed last week?
tyler_cracker is offline   Reply With Quote
Old 06-24-2011, 10:48 PM   #127
Pooh-Bah
 
TheIrishThug's Avatar
 
Join Date: Jan 2005
Location: Belligerent and numerous
Posts: 5,212
Re: ** Python Support Thread **

Quote:
Originally Posted by daveT View Post
May seem totally out of place, but the only thing I am against is using 'l' and '1' in the same program. People like to use various fonts and some of those fonts aren't going to show the difference between 'l' and '1'. Even with a decent font, it is still easy to mistaken the stuff.
l is a terrible variable name, you shouldn't be using it in the first place. Shame on jj for including it in his example.
TheIrishThug is offline   Reply With Quote
Old 06-25-2011, 11:47 AM   #128
Carpal \'Tunnel
 
jjshabado's Avatar
 
Join Date: Jul 2006
Posts: 11,034
Re: ** Python Support Thread **

I'm not sure if people get that the code I wrote was purely an example for this thread...

@tyler: Almost, since I'm combining it with a for loop. I would normally have just used filter, but I wanted to know what the "pythonic" way of doing it was.
jjshabado is offline   Reply With Quote
Old 06-25-2011, 01:26 PM   #129
Carpal \'Tunnel
 
tyler_cracker's Avatar
 
Join Date: Apr 2005
Location: Shallow End OTKP
Posts: 13,905
Re: ** Python Support Thread **

shabby,

just pointing out that this thread already contains some examples of pythonic ways to solve this problem.

although in other news i have to use python at work so i'm thinking about picking up scheme and SICP in my copious free time .
tyler_cracker is offline   Reply With Quote
Old 06-25-2011, 05:41 PM   #130
S.A.G.E. Master
 
daveT's Avatar
 
Join Date: Jun 2005
Location: Why didn't I use Clojure instead?
Posts: 16,799
Re: ** Python Support Thread **

Quote:
Originally Posted by jjshabado View Post
I'm not sure if people get that the code I wrote was purely an example for this thread...

@tyler: Almost, since I'm combining it with a for loop. I would normally have just used filter, but I wanted to know what the "pythonic" way of doing it was.
I know it was for example, I was just messing around with you. I know your ten times better than me, and this basic issue would likely not be a real-world example from you.

I was reading SO and came across this:

This throws and error instead of a code, which is more Pythonic IMO.

I laughed.

Quote:
Originally Posted by tyler_cracker View Post
shabby,

just pointing out that this thread already contains some examples of pythonic ways to solve this problem.

although in other news i have to use python at work so i'm thinking about picking up scheme and SICP in my copious free time .
Next class of ocw is in Scheme. I figure I'm mostly left to my own devices on this one. I am excited to learn it though.

But I'm on class number 9/24 and problemSet 6/13, so that's probably another 3 to 4 weeks.
daveT is offline   Reply With Quote
Old 06-25-2011, 05:57 PM   #131
Carpal \'Tunnel
 
jjshabado's Avatar
 
Join Date: Jul 2006
Posts: 11,034
Re: ** Python Support Thread **

Quote:
Originally Posted by tyler_cracker View Post
just pointing out that this thread already contains some examples of pythonic ways to solve this problem.
What is it though? List comprehensions aren't good when you want to embed 5 or 6 statements of logic. Something like list comprehension syntax for for loops would be cool, but that doesn't exist (that I know of).

Just curious if people had favorite ways of addressing this.
jjshabado is offline   Reply With Quote
Old 06-25-2011, 06:44 PM   #132
S.A.G.E. Master
 
daveT's Avatar
 
Join Date: Jun 2005
Location: Why didn't I use Clojure instead?
Posts: 16,799
Re: ** Python Support Thread **

Here's the python docs on list comprehensions, followed by nested list comprehensions.

http://docs.python.org/tutorial/data...comprehensions

and then there is a quick explanation that is a bit closer to English, and it uses FOR LOOPs:

http://www.greenteapress.com/thinkpy...ml/chap08.html

I think that option one looks cleaner.
daveT is offline   Reply With Quote
Old 06-25-2011, 08:45 PM   #133
S.A.G.E. Master
 
daveT's Avatar
 
Join Date: Jun 2005
Location: Why didn't I use Clojure instead?
Posts: 16,799
Re: ** Python Support Thread **

Had to do a problem where the teachers conveniently added their own test codes. I was really proud of myself for creating a nice eloquent solution that only took one parameter. The test code is this cheesy thing:

Code:
def test_get_word_score():
    """
    Unit test for get_word_score
    """
    failure=False
    # dictionary of words and scores
    words = {("", 7):0, ("it", 7):2, ("was", 7):6, ("scored", 7):9, ("waybill", 7):65, ("outgnaw", 7):61, ("outgnawn", 8):62}
    for (word, n) in words.keys():
        score = get_word_score(word, n)
        if score != words[(word, n)]:
            print "FAILURE: test_get_word_score()"
            print "\tExpected", words[(word, n)], "points but got '" + str(score) + "' for word '" + word + "', n=" + str(n)
            failure=True
    if not failure:
        print "SUCCESS: test_get_word_score()"
and of course, I wrote this function which did tested every word as it is supposed to be tested:

Code:
def get_word_score(word, n):
    points = 0
    for letters in word:
        if letters == ' ':
            points = 0
            break
        x = SCRABBLE_LETTER_VALUES[letters]
        points = points + x
    if n >= 7:
        points = points + 50
    return points


##word = 'it'
##word = 'outgnawn'
##word = 'scored'
##word = ' '
##word = 'outgnaw'
##word = 'was'
##word = 'waybill'
##get_word_score(word, len(word))
(I know, the testing I created is lazy and horrible)

but end up having to change

Code:
if n >= 7:
to:

Code:
if len(word) >= 7 and n >= 7:
Just so I can pass the testing. Why? Apparently, they want to have the players type in how many letters they used rather than automate it? I doubt that. Why not automate the word-counting in the function and have one parameter?

and seriusly, Python:

Code:
if len(word) and n >= 7:
is 10x better. Mmmkay?

I feel oppressed.
daveT is offline   Reply With Quote
Old 06-26-2011, 04:24 PM   #134
S.A.G.E. Master
 
daveT's Avatar
 
Join Date: Jun 2005
Location: Why didn't I use Clojure instead?
Posts: 16,799
Re: ** Python Support Thread **

I have a dictionary problem (written in pseudocode):

Code:
d = {'a':1, 'b':-2}

## want something like: 

if any d.value() < 0:
       do this
else: do that
not sure if something like this is possible.
daveT is offline   Reply With Quote
Old 06-26-2011, 04:29 PM   #135
Carpal \'Tunnel
 
jjshabado's Avatar
 
Join Date: Jul 2006
Posts: 11,034
Re: ** Python Support Thread **

Code:
if any([ v < 0 for v in d.values() ]):
    print 'hey'
jjshabado is offline   Reply With Quote

Reply
      

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off



All times are GMT -4. The time now is 12:12 AM.


Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.6.0 ©2011, Crawlability, Inc.
Copyright © 2008-2010, Two Plus Two Interactive