473,320 Members | 1,939 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

min() & max() vs sorted()

Hi,

Some time after reading about Python 2.5 and how the built-in functions
'min' and 'max' will be getting a new 'key' argument, I wondered how
they would treat those cases where the keys were the same, for example:

L = ["four", "five"]
print min(L, key = len), max(L, key = len)

The result is:

('four', 'four')

I would've thought that min(...) should return the same as
sorted(...)[0] (which it does) and that max(...) should return the same
as sorted(...)[-1] (which it doesn't).

I think that's just down to a subtlety in the way that 'max' is written.

Sep 15 '06 #1
4 4667
[MRAB]
Some time after reading about Python 2.5 and how the built-in functions
'min' and 'max' will be getting a new 'key' argument, I wondered how
they would treat those cases where the keys were the same, for example:

L = ["four", "five"]
print min(L, key = len), max(L, key = len)

The result is:

('four', 'four')
min() and max() both work left-to-right, and return the minimal or
maximal element at the smallest index.
I would've thought that min(...) should return the same as
sorted(...)[0] (which it does)
It does, but only because Python's sort is stable, so that minimal
elements retain their original relative order. That implies that the
minimal element with smallest original index will end up at index 0
after sorting.
and that max(...) should return the same as sorted(...)[-1] (which it doesn't).
Right -- although I don't know why you'd expect that.
I think that's just down to a subtlety in the way that 'max' is written.
It's straightforward, skipping error cases:

def max(*args):
is_first_arg = True
for arg in args:
if is_first_arg:
winner = arg
is_first_arg = False
elif arg winner: # only difference in min() is "<" here instead
winner = arg
return winner
Sep 15 '06 #2

Tim Peters wrote:
[MRAB]
Some time after reading about Python 2.5 and how the built-in functions
'min' and 'max' will be getting a new 'key' argument, I wondered how
they would treat those cases where the keys were the same, for example:

L = ["four", "five"]
print min(L, key = len), max(L, key = len)

The result is:

('four', 'four')

min() and max() both work left-to-right, and return the minimal or
maximal element at the smallest index.
It doesn't say that in the documentation.
I would've thought that min(...) should return the same as
sorted(...)[0] (which it does)

It does, but only because Python's sort is stable, so that minimal
elements retain their original relative order. That implies that the
minimal element with smallest original index will end up at index 0
after sorting.
and that max(...) should return the same as sorted(...)[-1] (which it doesn't).

Right -- although I don't know why you'd expect that.
Strings have index(), find(), etc which work left-to-right and
rindex(), rfind(), etc which work right-to-left.

Lists have index() but not rindex().

I just thought that if min() and max() work left-to-right then for
completeness there should also be rmin() and rmax(); alternatively,
min() should return sorted()[0] and max() should return sorted()[-1]
for symmetry (my personal preference).

Sep 15 '06 #3
[MRAB]
>>Some time after reading about Python 2.5 and how the built-in functions
'min' and 'max' will be getting a new 'key' argument, I wondered how
they would treat those cases where the keys were the same, for example:

L = ["four", "five"]
print min(L, key = len), max(L, key = len)

The result is:

('four', 'four')
[Tim Peters]
>min() and max() both work left-to-right, and return the minimal or
maximal element at the smallest index.
[MRAB]
It doesn't say that in the documentation.
Right, the /language/ doesn't define anything about which specific
minimal or maximal element is returned. Since you specifically
mentioned Python 2.5, I figured you were asking about CPython -- which
has always behaved in the way I explained.
>>I would've thought that min(...) should return the same as
sorted(...)[0] (which it does)
>It does, but only because Python's sort is stable, so that minimal
elements retain their original relative order. That implies that the
minimal element with smallest original index will end up at index 0
after sorting.
>>and that max(...) should return the same as sorted(...)[-1] (which
it doesn't).
>Right -- although I don't know why you'd expect that.
Strings have index(), find(), etc which work left-to-right and
rindex(), rfind(), etc which work right-to-left.

Lists have index() but not rindex().

I just thought that if min() and max() work left-to-right then for
completeness there should also be rmin() and rmax(); alternatively,
min() should return sorted()[0] and max() should return sorted()[-1]
for symmetry (my personal preference).
If you were to make either of those a feature request, I don't expect
they'd gain traction -- I expect "who cares?" would be the common
challenge, and "I do" wouldn't silence it ;-) Compelling use cases
sometimes work to get a new feature, but "completeness" or "symmetry"
almost never do on their own (they function more as sanity checks on
proposed solutions to use cases).
Sep 16 '06 #4
MRAB wrote:
Tim Peters wrote:
[MRAB]
Some time after reading about Python 2.5 and how the built-in functions
'min' and 'max' will be getting a new 'key' argument, I wondered how
they would treat those cases where the keys were the same, for example:
>
L = ["four", "five"]
print min(L, key = len), max(L, key = len)
>
The result is:
>
('four', 'four')
min() and max() both work left-to-right, and return the minimal or
maximal element at the smallest index.
It doesn't say that in the documentation.
I would've thought that min(...) should return the same as
sorted(...)[0] (which it does)
It does, but only because Python's sort is stable, so that minimal
elements retain their original relative order. That implies that the
minimal element with smallest original index will end up at index 0
after sorting.
and that max(...) should return the same as sorted(...)[-1] (which it doesn't).
Right -- although I don't know why you'd expect that.
Strings have index(), find(), etc which work left-to-right and
rindex(), rfind(), etc which work right-to-left.

Lists have index() but not rindex().

I just thought that if min() and max() work left-to-right then for
completeness there should also be rmin() and rmax(); alternatively,
min() should return sorted()[0] and max() should return sorted()[-1]
for symmetry (my personal preference).
Strange, but I've never thought of min and max in the way you do. If
anything I think of them as being distinct from sort; a way to get the
appropriate values without going to the bother of a full sort. After
doing my excercises on implementing sort routines, and reading about
cPythons sort algorithm, if I want just max or min, then I won't go
sorting in a hurry. I am quite happy with min and max just returning
the value.
With the addition of the key then I might want the option of returning
key(value) instead of value, i.e. 4 instead of "four".

- Paddy.

P.S. I have not played with Python 2.5 as yet

Sep 16 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Steven Bethard | last post by:
So I've been playing around with trying to add a keyword argument to min and max that works similarly to the one for sorted. It wasn't too hard actually, but it does raise a few questions about...
7
by: R0bert Nev1lle | last post by:
Internet Explorer always presents me with a challenge (partial repost). The current issue involves emulating max-width in IE using the technique described by Svend Tofte. ...
3
by: Ker | last post by:
I have a query that works great. It gives me the min for multiple fields. Within this query, I also need to get the max of some fields too. I currently have output of Date Name ...
5
by: kevinjouco | last post by:
Hello Have searched the group for a solution to the following problem without success: Table 1 has Ref No (No Duplicates) & Min Max Value Fields ie Ref No 1 Min 1 Max 10 Ref No 2 Min 11 Max...
11
by: lovecreatesbea... | last post by:
#define MIN(x, y) ((x)<(y) ? (x):(y)) Can a version without conditional operator of MIN macro be written?
4
by: BoscoPippa | last post by:
Hi all, I'm a rank beginner to C++ and programming in general. I'm in week 6 of my first course, and we have an assignment I'm having a little trouble with. If it matters, we're using standard...
38
by: copx | last post by:
Are the macros min() and max() part of stdlib.h or not? (according to the standard?) I have the following problem: I defined my own min() / max() macros because my compiler (MinGW) does NOT...
19
by: Eugeny Myunster | last post by:
I know, only simple one: #include <stdio.h> int main() { int min=0,max=0,i,arr; for(i=0;i<12;i++) arr=rand()%31-10; for(i=0;i<12;i++)
6
by: rahulsengupta895 | last post by:
. #define MIN(a,b) (a<b?a:b) #define MAX(a,b) (a>b?a:b) #include "Video.h" #define NO_HUE -1
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.