Newbie: finding the key/index of the min/max element 
Author Message
 Newbie: finding the key/index of the min/max element

In my quest of becoming a "Pythonistas" (where does this word come from BTW?),
here comes my 2nd trivial question: how do you find the key/index of the
max/min element in a sequence/map? I know you can call max(l) but this only
gives you the element itself, not the key. I also know it's not that hard to
code it up the hard way (for k in d.keys(): ... # do comparison), but is
there a more elegant way of doing this?

Thanks,

Ben



Sun, 17 Oct 2004 04:20:00 GMT  
 Newbie: finding the key/index of the min/max element

Quote:
> how do you find the key/index of the max/min element in a

sequence/map?

In python tuples are compared element-wise from first to last. So, e.g.:

Quote:
>>> min((1, 2, 3), (1, 3, 2))

(1, 2, 3)

So what you need is to represent your dictionary as a list of tuples
with the values first and keys second. Here is one way to do that in
recent versions of Python:

Quote:
>>> d = {'one' : 1, 'two' : 2, 'three' : 3, 'four' : 4, 'five' : 5}
>>> invertedList = zip(d.values(), d.keys())
>>> invertedList

[(4, 'four'), (3, 'three'), (5, 'five'), (2, 'two'), (1, 'one')]

Then min/max can operate on that list:

Quote:
>>> min(invertedList)
(1, 'one')
>>> max(invertedList)[1]

'five'

Jimmy



Sun, 17 Oct 2004 04:54:25 GMT  
 Newbie: finding the key/index of the min/max element

Quote:

> In my quest of becoming a "Pythonistas" (where does this word come from

I think it's plural, and the singular "Pythonista".

Quote:
> BTW?), here comes my 2nd trivial question: how do you find the key/index
> of the max/min element in a sequence/map? I know you can call max(l) but
> this only gives you the element itself, not the key. I also know it's not
> that hard to code it up the hard way (for k in d.keys(): ... # do
> comparison), but is there a more elegant way of doing this?

For a list, you _might_ do L.index(max(L)) -- that's conceptually two
passes on the list, but performance should still be OK.

This doesn't work for a mapping, though.  However, for a mapping (assuming
Python 2.2, else you need to iterate on D.keys() rather than on D):

        max([ (D[key], key) for key in D ])[1]

works (a variation of the DSU pattern -- we could call it DEU, for
Decorate, Extremal, Undecorate:-).

As to whether this is "more elegant" than a loop -- I'll pass.

Alex



Sun, 17 Oct 2004 05:23:05 GMT  
 Newbie: finding the key/index of the min/max element

Quote:

> In my quest of becoming a "Pythonistas" (where does this word come from BTW?),

The term derrives from the Sandinistas in Nicaragua, the Communist rebels who
fought against the US-installed conservative Somoza dictatorship (and others).
The suffix is sometimes used to refere to any contrarian group, and in cases
simply any group.

Quote:
> here comes my 2nd trivial question: how do you find the key/index of the
> max/min element in a sequence/map? I know you can call max(l) but this only
> gives you the element itself, not the key. I also know it's not that hard to
> code it up the hard way (for k in d.keys(): ... # do comparison), but is
> there a more elegant way of doing this?

I have trouble thinking of a clearer or more efficient way than simply iterating
on the list.

There's a lot of other ways to do it if you goal is to get rid of the for loop,
but I think they're all less efficient and I'm not sure you could say any are more
elegant.

E.g., you could say

    x = min( list )
    minkey = list.index( x )

and do it in "two steps".  But this actually requires on average 1.5 comparisons
on the list, one to find the min and however many checks it takes to find that
value again.

Then too, for small lists performance need not be a consideration.  However, I
personally would avoid obfuscating the matter with auxilliary lists or data
structures, even though it's insanely easy to do.

Sometimes a cigar is simply a cigar.

Regards

--jb

--
James J. Besemer  503-280-0838 voice
http://cascade-sys.com  503-280-0375 fax



Sun, 17 Oct 2004 05:36:58 GMT  
 Newbie: finding the key/index of the min/max element

Quote:


> > In my quest of becoming a "Pythonistas" (where does this word come from BTW
> ?),

> The term derrives from the Sandinistas in Nicaragua, the Communist rebels who
> fought against the US-installed conservative Somoza dictatorship (and others)
> .
> The suffix is sometimes used to refere to any contrarian group, and in cases
> simply any group.

That is simple ignorant American narrow-minded foolishness talking.
Normally, I would take extreme great care to not say anything so
offensive, but I have a deep suspcion that James J Besemer is one
of the 'can't take the heat' folks, and will learn to like a kinder
and gentler place than the one he proposes to make for Raymond Hettinger
if he finds out what it is like to live in the place he proposed.

At any rate, idiot fool deluded with the idea that the American perspective
is somehow comprehensive,

'ista' by those of us who speak Spanish means one hell of a lot
broader than that.  And not the same broadness in every Spanish
speaking nation.  My Spanish comes from where I lived in childhood,
San Pedro Sula, Honduras, and thus is fairly close to that of
Nicauragua (the country next door).  <except I lived in Cuenca Equador
also, complicated stuff my childhood>.  What 'ista' means varies from
sub-region to sub-region, and I get into confusion using it as I am
used to when I speak with Spaniards, where the rules are different --
but -- at any rate, in Honduras, it is used for everything from
'following the teaching of' to 'liking'.  You get 'istas' for every
damn fad on the planet, every popular song, let alone songwriter, and
just preferring the colour blue.

All of this has been going on for vastly longer than Super-Power
sponsored hell in small countries.

The Super-Power sponsored hell was such a vileness, that nobody would
be particularily interested in invoking that image.  Pythonista may
resonate to ignorant Americans with only that sound, but the rest of
us have a richer cultural experience in which to ground such an
expression.  At least by God I hope so -- else we have finally
reached the Nadir of Disneyism, where {*filter*} repression followed with
anarchy, and the dying hopes and dreams of a generation are _glorified_
because they are BRANDED.

Yours most disgustedly,
Laura Creighton



Sun, 17 Oct 2004 07:00:46 GMT  
 Newbie: finding the key/index of the min/max element

Quote:


> > > In my quest of becoming a "Pythonistas" (where does this word come from BTW
> > ?),

> > The term derrives from the Sandinistas in Nicaragua, the Communist rebels who
> > fought against the US-installed conservative Somoza dictatorship (and others)
> > .
> > The suffix is sometimes used to refere to any contrarian group, and in cases
> > simply any group.

> That is simple ignorant American narrow-minded foolishness talking.
> Normally, I would take extreme great care to not say anything so
> offensive, but I have a deep suspcion that James J Besemer is one
> of the 'can't take the heat' folks, and will learn to like a kinder
> and gentler place than the one he proposes to make for Raymond Hettinger
> if he finds out what it is like to live in the place he proposed.

one moment, please. You are making some valid points but i don't
see any sense in this 'eye for an eye' attitude.
After all beeing ignorant about something and the general
wish to collaboratively advance is the reason we are subscribed
on this list, isn't it? Why not explain your 'ista' origins to the
*poster of the original question*?

If many pythonistas would react to peoples (dumb) questions
and (ignorant) suggestions (including mine) within the scheme
of your posting, many of us might have quit programming alltogether
instead of enjoying the wonders of python and its community.

Sure enough i share your general criticism on US-politics and
especially their foreign politics.  But i don't think that
*anything* is won if you try to hammer it into somebodies head.

regards,

    holger



Sun, 17 Oct 2004 08:14:07 GMT  
 Newbie: finding the key/index of the min/max element

Quote:


> > The term derrives from the Sandinistas in Nicaragua, the Communist rebels who
> > fought against the US-installed conservative Somoza dictatorship (and others).

> > The suffix is sometimes used to refere to any contrarian group, and in cases
> > simply any group.

> That is simple ignorant American narrow-minded foolishness talking.
> Normally, I would take extreme great care to not say anything so
> offensive, but I have a deep suspcion that James J Besemer is one
> of the 'can't take the heat' folks, and will learn to like a kinder
> and gentler place than the one he proposes to make for Raymond Hettinger
> if he finds out what it is like to live in the place he proposed.

I'm truly sorry that my sincere attempt to answer what I thought was a simple
question caused you to resort to open hostility.  I am truly impressed by your
vicisousness.

Once again by innocently describing a true relationship I have observed between two
items (words in this case) puts be at loggerheads with someone who knows a little
more than me on the subject.

Quote:
> At any rate, idiot fool deluded with the idea that the American perspective
> is somehow comprehensive,

I did not presume to present an "American perspective" nor did I presume to be
comprehensive.  Nor did I suggest or imply that either was the case.

Without suggesting it's a good thing, I humbly submit that for many Americans they
do recognize the 'ista" mainly from the Sandinista context.  They don't do this
because of any political association or assumption of American pride but merely
because it's the first place or one of the few places most of them heard the term.

Quote:
> 'ista' by those of us who speak Spanish means one hell of a lot
> broader than that.  And not the same broadness in every Spanish
> speaking nation.  My Spanish comes from where I lived in childhood,
> San Pedro Sula, Honduras, and thus is fairly close to that of
> Nicauragua (the country next door).  <except I lived in Cuenca Equador
> also, complicated stuff my childhood>.  What 'ista' means varies from
> sub-region to sub-region, and I get into confusion using it as I am
> used to when I speak with Spaniards, where the rules are different --
> but -- at any rate, in Honduras, it is used for everything from
> 'following the teaching of' to 'liking'.  You get 'istas' for every
> damn fad on the planet, every popular song, let alone songwriter, and
> just preferring the colour blue.

Thank you for adding something truly informative to the topic.

I submit that if you calm down and put your emotions aside there is not a lot of
what I said that is factually inconsistent with your reality.  The second half of
my explanation particularly seems to resonate with your definition.  In all
fairness, I think I was more incomplete than factually in error.  And based on your
description, this "ista" suffix seems to be a rather complex and subtle idiom that
you admit even confuses you sometimes.  I suspect most non-Spanish speaking people,
Americans or otherwise, would be ignorant of most of this nuance.

Quote:
> All of this has been going on for vastly longer than Super-Power
> sponsored hell in small countries.

Nothing in my statement was intended to support or defend my country's behavior in
South America or anywhere else in the world.  From what I have learned over the
years, it's pretty much indefensible.  If you wanna talk politics, you may find we
have a lot more in common than you may imagine.

Quote:
> The Super-Power sponsored hell was such a vileness, that nobody would
> be particularily interested in invoking that image.  Pythonista may
> resonate to ignorant Americans with only that sound, but the rest of
> us have a richer cultural experience in which to ground such an
> expression.  At least by God I hope so -- else we have finally
> reached the Nadir of Disneyism, where {*filter*} repression followed with
> anarchy, and the dying hopes and dreams of a generation are _glorified_
> because they are BRANDED.

> Yours most disgustedly,
> Laura Creighton

I did not intend to invoke any particular image, only to document a phonetic
relationship between words.  With you, I obviously struck a nerve.  Clearly in some
way you were deeply wounded by what all happend down there.  I certainly had no
intention of invoking a hell from your past.  I hope you will forgive me.

I also sense that you are pissed at me personally, which puzzles me just a little.
This is the second time you have said offensive things to me though I have never
directed any comments whatsoever at you.  Perhaps you have a chip on your shoulder
about all Americans or men or whatever.  Maybe I remind you of somebody you hated.
I don't know.  I like to think I have shown that I can roll with the punches and I
feel I've made a few new friends on this list (some not even Americans).  I
resolved this morning to try and be more civil, in large part due to your note
yesterday.  Not out of an inability to "take the heat" it but because you made a
good point which was hard to disagree with.  However, your continuing to attack me
like this is not a way to encourage me to try and be civil.  Your emotionalism left
you open for a large number of smart-ass remarks, which I respectfully and with
difficulty suppresed.

Finally, I am truly puzzled by your Raymond Hettinger reference.  Although I
strongly disagree with him, I thought my very sincere reply was respectful and
deferential rather than hostile, and I thought it was very much in line with much
of the nit-picking that goes on here.  I certainly don't wish him any ill will, and
I will cheerfully support any good proposals he puts forth.  But his justification
for his proposal strikes me as pretty empty, as I indicated.  How does one properly
register dissent in your world?  Am I just supposed to blithly agree with
everything that is said?  Seems that would certainly put me in a distinct
minority.  If I'm missing something I trust you will point it out.

Oop.  Gotta go fix dinner for the family.

Sincerely,

--jb

--
James J. Besemer  503-280-0838 voice
http://www.*-*-*.com/  503-280-0375 fax



Sun, 17 Oct 2004 10:13:58 GMT  
 Newbie: finding the key/index of the min/max element
        ...

Quote:
> based on your description, this "ista" suffix seems to be a rather complex
> and subtle idiom that
> you admit even confuses you sometimes.  I suspect most non-Spanish
> speaking people, Americans or otherwise, would be ignorant of most of this
> nuance.

An '-ista' suffix is just as common in Italian as in Spanish.  The Italian
equivalent for most English words with an '-ist' suffix has it: egoista,
altruista, idealista, imperialista.  (Not *all* -- we don't say
'scientista', 'chimista', 'fisicista', but rather 'scienziato', 'chimico',
'fisico').  Plus, we use it for many words related to jobs (a few also have
'-ist' in English, e.g. 'dentista'): tassista is a taxi driver, fiorista a
seller of flowers, barista a bartender.

It's so widely use (and the exceptions so appearingly random -- I have no
idea "why" 'farmacista' is a common word but 'biologista' doesn't exist
and 'biologo' is used instead) that it's hard to define "the nuance".  I
suspect each variety of Spanish has similar issues.  Anyway, just wanted
to point out that a LOT of 'non-Spanish speaking people' have their own
connotations for the '-ista' suffix -- at least all Italian-speaking ones
(and I think Portuguese-speakers too -- isn't "Paulista" somebody from the
city of Sao Paulo, Brazil?).

Alex



Sun, 17 Oct 2004 15:43:10 GMT  
 Newbie: finding the key/index of the min/max element

Quote:

> (and I think Portuguese-speakers too -- isn't "Paulista"
> somebody from the city of Sao Paulo, Brazil?).

Indeed. In Portuguese there is also "istano" - in fact, "sou Paulista" means
"I am from the city of Sao Paulo" whereas "sou Paulistano" means "I am from
the state of Sao Paulo (specifically excluding the city)". Similarly "sou
Corinthiano" - "I am a supporter of the Corithians football team" (and if
anyone says "sou Palmeirense" ... eu te odeio ;)

Slang is fun, isn't it?

The uses of -ista in Portuguese correspond to those in Spanish and Italian -
it's used for professions, groups, etc. But there are lots of exceptions.

Tim Delaney



Sun, 17 Oct 2004 16:00:54 GMT  
 Newbie: finding the key/index of the min/max element

Quote:



> > > The term derrives from the Sandinistas in Nicaragua, the Communist rebels
>  who
> > > fought against the US-installed conservative Somoza dictatorship (and oth
> ers).

> > > The suffix is sometimes used to refere to any contrarian group, and in ca
> ses
> > > simply any group.

> > That is simple ignorant American narrow-minded foolishness talking.
> > Normally, I would take extreme great care to not say anything so
> > offensive, but I have a deep suspcion that James J Besemer is one
> > of the 'can't take the heat' folks, and will learn to like a kinder
> > and gentler place than the one he proposes to make for Raymond Hettinger
> > if he finds out what it is like to live in the place he proposed.

> I'm truly sorry that my sincere attempt to answer what I thought was a simple
> question caused you to resort to open hostility.  I am truly impressed by you
> r
> vicisousness.

Oh Rats!  If you are _impressed_ by this, then I will have to try something
else. I wanted you to be _disgusted_.  One more marketting plan shot to
hell...

Quote:

> Once again by innocently describing a true relationship I have observed betwe
> en two
> items (words in this case) puts be at loggerheads with someone who knows a li
> ttle
> more than me on the subject.

Yes.  And the usual thing around here is for the more knowledgable to teach
the less knowledgable so that everybody benefits.  There are other
newsgroups where the usual thing is for the people with the knowledge
to go feed their egos by showing off how much they know, and by denigrating
those who know less as if ignorance was something to be ashamed of.

We know all about where that goes, and we don't want to go there.  People
have been saying that if Python ever becomes popular, comp.lang.python
will turn into comp.lang.perl.  We have been told repeatedly that there is
no way to build consensus on the internet, because there are too many
blunt speakers, like you, who actually think that it is a good idea to
_discipline_ (here used in its sense of _punish_) learners to not explore
half-baked ideas and thoughts using rudeness and sarcasm.

<snipped -- me trying to make mince-meat of James Besemer, who doesn't
 mince easily.>

- Show quoted text -

Quote:
> I did not intend to invoke any particular image, only to document a phonetic
> relationship between words.  With you, I obviously struck a nerve.  Clearly i
> n some
> way you were deeply wounded by what all happend down there.  I certainly had
> no
> intention of invoking a hell from your past.  I hope you will forgive me



Sun, 17 Oct 2004 15:19:41 GMT  
 Newbie: finding the key/index of the min/max element

Quote:



> > > The term derrives from the Sandinistas in Nicaragua, the Communist rebels
>  who
> > > fought against the US-installed conservative Somoza dictatorship (and oth
> ers).

> > > The suffix is sometimes used to refere to any contrarian group, and in ca
> ses
> > > simply any group.

> > That is simple ignorant American narrow-minded foolishness talking.
> > Normally, I would take extreme great care to not say anything so
> > offensive, but I have a deep suspcion that James J Besemer is one
> > of the 'can't take the heat' folks, and will learn to like a kinder
> > and gentler place than the one he proposes to make for Raymond Hettinger
> > if he finds out what it is like to live in the place he proposed.

> I'm truly sorry that my sincere attempt to answer what I thought was a simple
> question caused you to resort to open hostility.  I am truly impressed by you
> r
> vicisousness.

Oh Rats!  If you are _impressed_ by this, then I will have to try something
else. I wanted you to be _disgusted_.  One more marketting plan shot to
hell...

Quote:

> Once again by innocently describing a true relationship I have observed betwe
> en two
> items (words in this case) puts be at loggerheads with someone who knows a li
> ttle
> more than me on the subject.

Yes.  And the usual thing around here is for the more knowledgable to teach
the less knowledgable so that everybody benefits.  There are other
newsgroups where the usual thing is for the people with the knowledge
to go feed their egos by showing off how much they know, and by denigrating
those who know less as if ignorance was something to be ashamed of.

We know all about where that goes, and we don't want to go there.  People
have been saying that if Python ever becomes popular, comp.lang.python
will turn into comp.lang.perl.  We have been told repeatedly that there is
no way to build consensus on the internet, because there are too many
blunt speakers, like you, who actually think that it is a good idea to
_discipline_ (here used in its sense of _punish_) learners to not explore
half-baked ideas and thoughts using rudeness and sarcasm.

<snipped -- me trying to make mince-meat of James Besemer, who doesn't
 mince easily.>

Quote:
> I did not intend to invoke any particular image, only to document a phonetic
> relationship between words.  With you, I obviously struck a nerve.  Clearly i
> n some
> way you were deeply wounded by what all happend down there.  I certainly had
> no
> intention of invoking a hell from your past.  I hope you will forgive me.

Apology accepted.

Quote:
> I also sense that you are pissed at me personally, which puzzles me just a li
> ttle.
> This is the second time you have said offensive things to me though I have ne
> ver
> directed any comments whatsoever at you.  Perhaps you have a chip on your sho
> ulder
> about all Americans or men or whatever.  

You need to learn to read personal attacks a little more carefully.  In the
n-1 I sent you, among other things, I blasted you for claiming to speak
for all Americans.  While not conclusive, that would provide certain
support to the idea that it is not _all_ Americans I dislike.

All of this was personal.  You can't hide behind 'other men' or 'other
Americans'.  Francois Pinard, Cliff Wells, Steve Holden, Tim Delaney,
I, and I forget who else who helped are not going to sit around idlely
while you turn c.l.p into a 'sarcasm friendly' place.  We will fight
you over this.  This is an extremely interesting and to my mind
important social experiement, and you aren't going to find many people
who will support you in your efforts to turn this place into just
another newsgroup.

If you want one of those, make your own gatewayed mailing list.  

Quote:
> Maybe I remind you of somebody you h
> ated.
> I don't know.  I like to think I have shown that I can roll with the punches
> and I
> feel I've made a few new friends on this list (some not even Americans).  I
> resolved this morning to try and be more civil, in large part due to your not
> e
> yesterday.

Wow!  Thank you so much!  Then it did work. Interesting.  You admired me for
my viciousness, and then resolved to behave better.  Now that was an outcome
I hadn't predicted.  I am going to have to think about this some more.

 Not out of an inability to "take the heat" it but because you mad

Quote:
> e a
> good point which was hard to disagree with.  However, your continuing to atta
> ck me
> like this is not a way to encourage me to try and be civil.  

Aha.  This is why we don't like sarcasm around here.  Its an attack.  It works
the same way.

Quote:
> Your emotionalis
> m left
> you open for a large number of smart-ass remarks, which I respectfully and wi
> th
> difficulty suppresed.

Yes.  This is a common problem in other newsgroups.  Somebody gets off
one sarcastic crack, and then says 'there, there, calm down, I am being
reasonable now'.  Thus we create a climate where everybody competes to
get the first sarcastic comment off.  This leads to comp.lang.perl as well.

Quote:

> Finally, I am truly puzzled by your Raymond Hettinger reference.  Although I
> strongly disagree with him, I thought my very sincere reply was respectful an
> d
> deferential rather than hostile, and I thought it was very much in line with
> much
> of the nit-picking that goes on here.  I certainly don't wish him any ill wil
> l, and
> I will cheerfully support any good proposals he puts forth.  But his justific
> ation
> for his proposal strikes me as pretty empty, as I indicated.  How does one pr
> operly
> register dissent in your world?  Am I just supposed to blithly agree with
> everything that is said?  Seems that would certainly put me in a distinct
> minority.  If I'm missing something I trust you will point it out.

Lumberjack is a Troll, or quite possibly the alter-ego of one of us.
Right now I don't think that Lumberjack is a problem, and ignoring
Trolls is one of the things we do around here (though sometimes we
have to be reminded of that).  You, however, are a large problem.  You
are proposing to make this a sarcasm-friendly place.  You want us to
lighten-up and toughen-up simultaneously.  You want us to change our
societal norms (hmm, can we consider c.l.p a society?  It sure is a
F?rening, <Swedish for Society>, but do you get to use English that
way?) and our entire process for your sake.  You want to make c.l.p a
place where people do not post for fear of _wasting your time_.

That is monstrous egoism, and I don't even think you are aware of it.
Use your n key like everybody else, or learn patience, or stop reading
c.l.p.  Just get the weekly URL.  Or make other arrangements.  I am
currently reading c.l.p for 18 people, forwarding the articles that
would interest them to my colleagues.  That way only one of us has to
read the whole newsgroup.

Be creative, inventive, and stop stomping all over our social
experiment.  Among other things, we are training people here, and you
can't truly train people to program well unless they can completely
and unself-consciously present themselves and their works, just being
themselves, doing the best they can.  This is most overwhelming true
when 'the best they can' is objectively 'not very good at all'.  It
takes time for the unskilled to become skilled.  But that is precisely
what the people of c.l.p are giving to each other.  Our valuable time.
Because we think time spent with people here, learning things and helping
other people learn things is _valuable_.

And when we are too busy, we kill threads, or even stop reading news
for a while.  But with the quiet joy and satisfaction that we get knowing
that, however busy we are, somebody in c.l.p. with more time is teaching
somebody else something, right now.

All the busy occasional posters you see here -- old regulars who are now
way too busy -- we don't want them to find comp.lang.perl when they come
back.  That will mean that we failed to manage the space properly.

Quote:

> Oop.  Gotta go fix dinner for the family.

Hope it was good.

Quote:

> Sincerely,

> --jb

Laura


Sun, 17 Oct 2004 15:19:41 GMT  
 Newbie: finding the key/index of the min/max element
        ...

Quote:
> loop, but I think they're all less efficient and I'm not sure you could
> say any are more elegant.

> E.g., you could say

>     x = min( list )
>     minkey = list.index( x )

> and do it in "two steps".  But this actually requires on average 1.5
> comparisons on the list, one to find the min and however many checks it
> takes to find that value again.

> Then too, for small lists performance need not be a consideration.

Right, but efficiency generally needs _measuring_.  Guesses are no good.

import time, sys, random

def imax_loop(seq):
    cmax = None
    i = 0
    for item in seq:
        if cmax is None or item>cmax:
            cmax = item
            imax = i
        i = i + 1
    return imax

def imax_noloop(seq):
    return seq.index(max(seq))

def imax_zip(seq):
    _idx = xrange(sys.maxint)
    return max(zip(seq,_idx))[1]

r = range(100000)
for i in range(4):
    random.shuffle(r)
    for f in imax_loop, imax_noloop, imax_zip:
        start = time.clock()
        x = f(r)
        stend = time.clock()
        print f.__name__,stend-start


imax_loop 0.12
imax_noloop 0.04
imax_zip 0.73
imax_loop 0.11
imax_noloop 0.05
imax_zip 0.69
imax_loop 0.1
imax_noloop 0.05
imax_zip 0.7
imax_loop 0.1
imax_noloop 0.05
imax_zip 0.69

So, performance doesn't really matter much -- but in as much as it does,
the "noloop" approach wins hands down, taking 1/2 to 1/3 the time of
the "obvious" loop (forget the zip approach, clearly).

Since seq.index(max(seq)) is most concise, fastest, and quite clear,
I think it qualifies for "the one obvious way to do it" in this case.
Pity it doesn't work for mappings, only for sequences...

Alex



Sun, 17 Oct 2004 16:42:15 GMT  
 Newbie: finding the key/index of the min/max element
        ...

Quote:
> All of this was personal.  You can't hide behind 'other men' or 'other
> Americans'.  Francois Pinard, Cliff Wells, Steve Holden, Tim Delaney,
> I, and I forget who else who helped are not going to sit around idlely
> while you turn c.l.p into a 'sarcasm friendly' place.  We will fight
> you over this.  This is an extremely interesting and to my mind
> important social experiement, and you aren't going to find many people
> who will support you in your efforts to turn this place into just
> another newsgroup.

Actually, _I_ wouldn't mind it if there was a way to discourage people
from starting Yet Another Round of language-change proposals or whines
and complaints against some aspect of the language they have not
carefully considered.  "I don't understand why 'split' should be a
method of the joiner rather than of the sequence" is one thing, and,
even though often explored in the past, deserves courteous response if
one has the time and patience to repeat it (or pointing to a FAQ, etc).

But too often you see the very different attitude of "I _don't like_
the fact that 'join' is a method of the joiner rather than of the
sequence", all the way up the scale of increasing arrogance to "I
*know* that 'join' SHOULD be a method of the sequence rather than
of the joiner and thus you should change the language to suit my
whims".  It does seem to me that such arrogance is Excessively
Annoying Behavior, and if sarcastic and aggressive reactions could
discourage it, then I wouldn't mind.  I don't know if they can, as
much of it comes from people who can't be bothered to read previous
messages (obviously: they ignore arguments that have been made over
and over again!), and thus don't know about previous flamewars either.

_On a given thread_, however, _once reasonable and informative
responses have been given and wilfully ignored_, I can see the case
for lashing out (I can also see the opposite case as you present
it -- problem with being a Libra, this 360 degrees vision:-).  And
my instincts are surely to go for the jugular in such cases (while
my time-management reasonable self tells me I don't have TIME for
another flamewar -- but then, I don't have time to remind people
for the 1000th time that performance must be measured, not guessed
at, yet I do...:-).

I don't think it's at all reasonable to unleash attacks against
proposals you disagree with, just because you do disagree.  But
I just hope I'll be in one of my time-management-necessitated
periods of abstinence from this group next time some newbie
arrogantly and querulously teaches us why Python should have
[a] explicit block delimiters, [b] join as a method of all the
possible sequences and not of joiner-objects, [c] hygienic
macros, [d] booleans -- oops forget I mentioned the latter, since
they're now BDFL-blessed cruf^H^H^H^H brilliant innovations...:-).

Alex



Sun, 17 Oct 2004 17:07:02 GMT  
 Newbie: finding the key/index of the min/max element

Quote:

> I just hope I'll be in one of my time-management-necessitated
> periods of abstinence from this group next time some newbie
> arrogantly and querulously teaches us why Python should have
> [a] explicit block delimiters, [b] join as a method of all the
> possible sequences and not of joiner-objects, [c] hygienic
> macros, [d] booleans --

I'd like to add an [e] proposed, ironically, by one of this thread's
more vocal participants (amidst 800-odd lines of rhetorical
{*filter*}, juvenile muscle-flexing and snide remarks):

[e] A ternary operator of the form:
x = ( if 'superfluity' > 'ugliness' : 'insane' else: 'unpythonic' )



Sun, 17 Oct 2004 18:12:21 GMT  
 Newbie: finding the key/index of the min/max element

    >> I'm truly sorry that my sincere attempt to answer what I
    >> thought was a simple question caused you to resort to open
    >> hostility.  I am truly impressed by you r vicisousness.

    Laura> Oh Rats!  If you are _impressed_ by this, then I will have
    Laura> to try something else.  I wanted you to be _disgusted_.

The First Law of Marketing: It doesn't matter how bad the thing they
say about you is, as long as it isn't nothing.

But think about it -- this is logical.  By showing that you have
command of the idiom, you attract attention to yourself.  By the fact
that normally you _choose_ not to use it, your use in a special case
highlights your normal behavior, and invites consideration of your
motivation and philosophy.

It won't work on everybody.  But where it doesn't work, they won't
pause to be impressed.  They'll just dig a foxhole and start lobbing
grenades back.

    Laura> hmm, can we consider c.l.p a society?  It sure is a
    Laura> F?rening, <Swedish for Society>, but do you get to use
    Laura> English that way?)

Sure.  Many meanings for "society", this usage is certainly
acceptable.  However, you might prefer "community" (which sounds like
and is derived from "common", ie, "shared").

    Laura> This is an extremely interesting and to my mind important
    Laura> social experiement,

Is this documented somewhere?  I'm interested in the experiment.  The
newsgroups and mailing lists I participate in are by and large not so
different from c.l.py in this respect, so it was not obvious to me
that it is something different.

    Laura> and you aren't going to find many people who will support
    Laura> you in your efforts to turn this place into just another
    Laura> newsgroup.

    Laura> You want us to change our societal norms and our entire
    Laura> process for your sake.  You want to make c.l.p a place
    Laura> where people do not post for fear of _wasting your time_.

I think this is rather unfair.  I agree that this is the _effect_ of
this typically American form of behavior.  You have every right, and
if you like, a duty, to defend your experiment from this threat.

But what is _wanted_ is mostly simple freedom of expression.  "Those
words just roll off _my_ back; what's your problem?  They're _just_
_words_, OK?!" says the American.  Well, to many people they're not
"mere words".  But that takes some explaining to many of the people I
know.  Americans often ask "well, why can't I do it my way, and you do
it yours?  If people don't like my way, that's what score-files are
for."  

I don't have an answer to that for you, but I did want to present it
in a way that acknowledges both the threat you correctly perceive, and
the fact that it surely was intended as an expression of personal
style, not subversion of your social fabric.  And I would be
interested to hear the answer, although I intend to follow the rules
regardless of how they are justified.

--
Institute of Policy and Planning Sciences     http://turnbull.sk.tsukuba.ac.jp
University of Tsukuba                    Tennodai 1-1-1 Tsukuba 305-8573 JAPAN



Sun, 17 Oct 2004 19:50:55 GMT  
 
 [ 25 post ]  Go to page: [1] [2]

 Relevant Pages 

1. Fixing the FAQ (was Re: Newbie: finding the key/index of the min/max element)

2. Finding max and min values in a queue

3. Help | Finding min/max no. from a list

4. was Finding min/max no. from a list

5. Finding min values for array elements

6. Max size of a CDX index key

7. finding non-unique keys in index.

8. Min and Max values?

9. Novice needs help with SQL (Max,Min, etc) queries on RealDBs

10. How to change (Restore,Move,Size,Min,Max)

11. Min() or MAX()???

12. MIN and MAX are eating my head

 

 
Powered by phpBB® Forum Software