Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #76625 > unrolled thread
| Started by | Ben Finney <ben+python@benfinney.id.au> |
|---|---|
| First post | 2014-08-20 09:41 +1000 |
| Last post | 2014-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.
Re: More Pythonic implementation Ben Finney <ben+python@benfinney.id.au> - 2014-08-20 09:41 +1000
| From | Ben Finney <ben+python@benfinney.id.au> |
|---|---|
| Date | 2014-08-20 09:41 +1000 |
| Subject | Re: 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
Back to top | Article view | comp.lang.python
csiph-web