Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #76598

Re: More Pythonic implementation

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!ecngs!feeder2.ecngs.de!novso.com!newsfeed.xs4all.nl!newsfeed2.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <ckaynor@zindagigames.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.115
X-Spam-Level *
X-Spam-Evidence '*H*': 0.79; '*S*': 0.02; 'result,': 0.07; 'used.': 0.09; 'def': 0.12; 'iterable': 0.16; 'iterables': 0.16; 'poker': 0.16; 'provided),': 0.16; 'pythonic': 0.16; 'received:mail- wi0-f178.google.com': 0.16; 'segment': 0.16; 'subject:More': 0.16; 'wrote:': 0.18; 'things.': 0.19; 'value.': 0.19; 'aug': 0.22; 'to:name:python-list@python.org': 0.22; 'lets': 0.24; 'header:In- Reply-To:1': 0.27; 'function': 0.29; 'am,': 0.29; 'message- id:@mail.gmail.com': 0.30; 'code': 0.31; 'subject:skip:i 10': 0.31; 'another': 0.32; 'received:209.85.212': 0.32; 'received:209.85': 0.35; 'case,': 0.35; 'received:google.com': 0.35; 'returning': 0.36; 'two': 0.37; 'list': 0.37; 'received:209': 0.37; 'skip:& 10': 0.38; 'to:addr:python-list': 0.38; 'rather': 0.38; 'skip:& 20': 0.39; 'highest': 0.39; 'to:addr:python.org': 0.39; 'how': 0.40; 'first': 0.61; 'more': 0.64; 'different': 0.65; 'taking': 0.65; 'hand': 0.80; 'as:': 0.81; 'hand.': 0.84; 'rank.': 0.84; 'hand,': 0.93; 'rank': 0.93; 'hands': 0.96; 'ranking': 0.96; 'ranked': 0.97
X-Google-DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:in-reply-to:references:from:date :message-id:subject:to:content-type; bh=JHvZ5SRzcuUYSNcSMSNfYRjGWlQ8c95P4toz5x65h14=; b=DwmLppAlMrsEZlH9mrI+chO0DQDAWZGWPipvhx1fi+0vG5a/Xk91Z3ZKj9VaFCX0qs 5TbFfnlGH7W0drRevlk8wciRaj3JF2y/SjEfys1YhBtPwsFftjQRLPz0K2TMPC0qc4cL d63Gk9iBnd8WmmnlqVl1OOTpMCzbF837Juf+dsFjj9StuyYO6Nqq1uM2UY0dsIqd6rlQ ZDK42XR1RKSsdpi0QRYn2+KAKyIMhZzkiHYSUg25m/cY7kkFbSOZABtZ1/jAvgmvGcIW UjYSUkQsNjxEDbQh3RHBAgVjFhUMqoxHsrCVp3scLZsVLbzaw7BpjUqmXUvci0jIgFoJ CcBw==
X-Gm-Message-State ALoCoQlN7sAmBTax3369JtjoeKEkWcJaWOXdQNNcwJkqDnI1teoC8AkeUm+1pYWwbYD9TAGhrE+p
X-Received by 10.194.58.244 with SMTP id u20mr41930720wjq.36.1408474697080; Tue, 19 Aug 2014 11:58:17 -0700 (PDT)
MIME-Version 1.0
In-Reply-To <CAGOccLmG3UzUtn3+Dy1ADDvGD3P14GZp0V5s4_x-_9HRJeY2XA@mail.gmail.com>
References <CAGOccLmG3UzUtn3+Dy1ADDvGD3P14GZp0V5s4_x-_9HRJeY2XA@mail.gmail.com>
From Chris Kaynor <ckaynor@zindagigames.com>
Date Tue, 19 Aug 2014 11:57:56 -0700
Subject Re: More Pythonic implementation
To "python-list@python.org" <python-list@python.org>
Content-Type multipart/alternative; boundary=047d7b86cf26feb98c05010011e4
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.13163.1408474700.18130.python-list@python.org> (permalink)
Lines 104
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1408474700 news.xs4all.nl 2835 [2001:888:2000:d::a6]:57103
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:76598

Show key headers only | View raw


[Multipart message — attachments visible in raw view] - view raw

On Tue, Aug 19, 2014 at 10:09 AM, Shubham Tomar <tomarshubham24@gmail.com>
wrote:

> Lets say I have a function poker(hands) that takes a list of hands and
> returns the highest ranking hand, and another function hand_rank(hand) that
> takes hand and return its rank. As in Poker
> http://www.pokerstars.com/poker/games/rules/hand-rankings/.
>
> Which of the following is better and more Pythonic ?
>

Your two code segments will do different things.


>
> def poker(hands):
>     return max(hands, key=hand_rank)
>

In this case, the "hand_rank" function will take a single hand, and return
its rank value. Additionally, the "poker" function will return the highest
ranked hand.


>
> or
>
> def poker(hands):
>     return max(hand_rank(hands))
>

In this case, the "hand_rank" function will take an iterable of hands and
return an iterable of hand ranks. Additionally, the "poker" function will
return the rank of the highest ranked hand.

If that is the desired result, I would recommend writing this as:

def poker(hands):
    return max(map(hand_rank, hands))

which will result in hand_rank taking a single hand and returning its rank
value (similar to the first code segment you provided), rather than having
hand_rank deal with iterables of hands. However, that has more to do with
how you want hand_rank to behave, and where else it might be used.

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: More Pythonic implementation Chris Kaynor <ckaynor@zindagigames.com> - 2014-08-19 11:57 -0700

csiph-web