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: 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: References: From: Chris Kaynor Date: Tue, 19 Aug 2014 11:57:56 -0700 Subject: Re: More Pythonic implementation To: "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 List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: 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 --047d7b86cf26feb98c05010011e4 Content-Type: text/plain; charset=UTF-8 On Tue, Aug 19, 2014 at 10:09 AM, Shubham Tomar 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. --047d7b86cf26feb98c05010011e4 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable
On T= ue, Aug 19, 2014 at 10:09 AM, Shubham Tomar <tomarshubham24@gmail= .com> wrote:
Lets say I have a func= tion poker(hands) that takes a list of hands and returns the highest rankin= g hand, and another function hand_rank(hand) that takes hand and return its= rank. As in Poker=C2=A0http://www.pokerstars.com/poker/games= /rules/hand-rankings/.

Which of the following is better and more Pythonic ?

Your two code segments will do di= fferent things.
=C2=A0

def poker(hands):
=C2=A0 =C2= =A0 return max(hands, key=3Dhand_rank)
In this case, the "hand_rank" function will take a si= ngle hand, and return its rank value. Additionally, the "poker" f= unction will return the highest ranked hand.
=C2=A0

o= r=C2=A0

def poker(hands):
=C2=A0 =C2=A0 return max(hand_ra= nk(hands))

In this case, the "hand_rank" fu= nction will take an iterable of hands and return an iterable of hand ranks.= Additionally, the "poker" function will return the rank of the h= ighest ranked hand.

If that is the desired result, I would recommend writin= g this as:

def poker(hands):
=C2=A0 =C2= =A0 return max(map(hand_rank, hands))

which will r= esult in hand_rank taking a single hand and returning its rank value (simil= ar to the first code segment you provided), rather than having hand_rank de= al 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.
--047d7b86cf26feb98c05010011e4--