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


Groups > comp.lang.python > #76625 > unrolled thread

Re: More Pythonic implementation

Started byBen Finney <ben+python@benfinney.id.au>
First post2014-08-20 09:41 +1000
Last post2014-08-20 09:41 +1000
Articles 1 — 1 participant

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re: More Pythonic implementation Ben Finney <ben+python@benfinney.id.au> - 2014-08-20 09:41 +1000

#76625 — Re: More Pythonic implementation

FromBen Finney <ben+python@benfinney.id.au>
Date2014-08-20 09:41 +1000
SubjectRe: More Pythonic implementation
Message-ID<mailman.13182.1408491733.18130.python-list@python.org>
Shubham Tomar <tomarshubham24@gmail.com> writes:

> 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.

To make it clearer, I think you mean something like this::

    def hand_rank(hand):
        """ Determine the rank of the poker hand. """
        rank = int(some_complex_computation(hand))
        return rank

In other words, I'm assuming ‘hand_rank’ returns an integer.

> Which of the following is better and more Pythonic ?

Only one of them does anything useful :-)

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

This will return the item from the collection ‘hand’ with the maximum
value as determined by ‘hand_rank’. So this appears to do what you want.

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

This raises “TypeError: 'int' object is not iterable” because you're
operating on one return value from ‘hand_rank’, which is not a
collection.

-- 
 \       “Corporation, n. An ingenious device for obtaining individual |
  `\       profit without individual responsibility.” —Ambrose Bierce, |
_o__)                                   _The Devil's Dictionary_, 1906 |
Ben Finney

[toc] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web