Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!enother.net!enother.net!gegeweb.org!eternal-september.org!feeder.eternal-september.org!weretis.net!feeder1.news.weretis.net!news.tota-refugium.de!.POSTED!not-for-mail From: Thomas Rachel Newsgroups: comp.lang.python Subject: Re: More Pythonic implementation Date: Wed, 20 Aug 2014 17:52:14 +0200 Organization: A not so newly installed InterNetNews server Lines: 42 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Trace: tota-refugium.de 1408550404 27948 eJwNzLcRwEAIAMGW8DzlYPsvQYou2TllQ2sXUxM9PYKd/ZvMqeDvgtvRvEhiuFTDnzAyJMveAMygkOHWoK1PNwem1i8xSjiG/ku+w07qBx+AYh3l (20 Aug 2014 16:00:04 GMT) X-Complaints-To: abuse@news.tota-refugium.de NNTP-Posting-Date: Wed, 20 Aug 2014 16:00:04 +0000 (UTC) User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:24.0) Gecko/20100101 Thunderbird/24.6.0 Hamster/2.1.0.11 X-User-ID: eJwFwYkBgDAIA8CVJCY842CF/UfonV43P0GXU6tNVEjFetDwzzg9MYXjsB0acmr5Y9tU2bwDLRCE In-Reply-To: Cancel-Lock: sha1:dregspDTzsmJaJm5AU+ETkujBSI= X-NNTP-Posting-Host: eJwFwQcBwEAIBDBLjAMeOZThX0ITU2fvgJvDzi7XAsQcSppttz4PAfbjK+SBDiKpu7rhmKByqbetwtC2TNwq5RDq4pvrVyEdX3QPV/1SUR4w Xref: csiph.com comp.lang.python:76672 Am 19.08.2014 19:09 schrieb Shubham Tomar: > 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 ? They are different. > def poker(hands): > return max(hands, key=hand_rank) This one determines the hands element with the biggest hand_rank(i) value and returns it, while > def poker(hands): > return max(hand_rank(hands)) returns the hand_rank value, but only if hand_rank() is applicable to lists. Otherwise, it fails. hands = [1, 2, 3, 4, 5] def hand_rank(i): return -i So, max(hands, key=hand_rank) gives 1 because it has the biggest hand_rank(value) of -1 in the range of -5..-1. OTOH, max(hand_rank(hands)) will fail, because there is no list.__neg__(). If there was, it would be the same as max([-1, -2, -3, -4, -5]), so it would be -1 itself.