469,323 Members | 1,558 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,323 developers. It's quick & easy.

list index()

What's with the index() function of lists throwing an exception on not
found? Let's hope this is rectified in Python 3. If nothing else, add
a function that doesn't throw an exception. There are a million
situations where you can have an item not be in a list and it is not
an exception situation.

Aug 30 '07
96 4150
On Aug 31, 8:47 am, Erik Max Francis <m...@alcyone.comwrote:
Hendrik van Rooyen wrote:
weird this - maybe a native English speaker can comment -
when I pronounce what fishermen do - it rhymes with roll,
but when I am talking about the thing that lives under bridges
and munches goats, the "O" sound is shorter, and more
towards the back of my mouth.

Native English accents vary as well, but _roll_ rhymes with _troll_, not
_trawl_. _Trawl_ would rhyme with _fall_, and _fall_ definitely doesn't
rhyme with _roll_.

--
Erik Max Francis && m...@alcyone.com &&http://www.alcyone.com/max/
San Jose, CA, USA && 37 20 N 121 53 W && AIM, Y!M erikmaxfrancis
I do not like work even when someone else does it.
-- Mark Twain
I say the 'oll' in troll like the 'ol' in frolic, and pronounce roll
and role similarly.

My accent is probably from the East Midlands of the UK, but is not
pronounced.

- Paddy.

Aug 31 '07 #51
Paddy wrote:
I say the 'oll' in troll like the 'ol' in frolic, and pronounce roll
and role similarly.

My accent is probably from the East Midlands of the UK, but is not
pronounced.
_Troll_ and _frolic_ aren't pronounced with the same "o" sound in any
accent I've ever heard of. Which you pronounce _boat_ and _bot_ the
same way, too?

--
Erik Max Francis && ma*@alcyone.com && http://www.alcyone.com/max/
San Jose, CA, USA && 37 20 N 121 53 W && AIM, Y!M erikmaxfrancis
There are no dull subjects. There are only dull writers.
-- H.L. Mencken
Aug 31 '07 #52
Erik Max Francis wrote:
Paddy wrote:
>I say the 'oll' in troll like the 'ol' in frolic, and pronounce roll
and role similarly.

My accent is probably from the East Midlands of the UK, but is not
pronounced.

_Troll_ and _frolic_ aren't pronounced with the same "o" sound in any
accent I've ever heard of. Which you pronounce _boat_ and _bot_ the
same way, too?
[Amusingly contemplating a trolling war about the pronunciation of "troll"]

Well they sound the same in my more-or-less South London accent.
I can't write those funny phonetic symbols (and I hate to
imagine the Unicode encoding hoops I'd have to jump through
to make them readable anyway) but both "o"s sound short to me.
Like "bot" rather than "boat" using your example.

TJG
Aug 31 '07 #53
On 2007-08-31, Erik Max Francis <ma*@alcyone.comwrote:
>I say the 'oll' in troll like the 'ol' in frolic, and pronounce roll
and role similarly.

My accent is probably from the East Midlands of the UK, but is not
pronounced.

_Troll_ and _frolic_ aren't pronounced with the same "o" sound in any
accent I've ever heard of.
Welcome to England!
Which you pronounce _boat_ and _bot_ the same way, too?
No. HTH HAND.
Aug 31 '07 #54
Tim Golden wrote:
Erik Max Francis wrote:
>Paddy wrote:
>>I say the 'oll' in troll like the 'ol' in frolic, and pronounce roll
and role similarly.

My accent is probably from the East Midlands of the UK, but is not
pronounced.
_Troll_ and _frolic_ aren't pronounced with the same "o" sound in any
accent I've ever heard of. Which you pronounce _boat_ and _bot_ the
same way, too?

[Amusingly contemplating a trolling war about the pronunciation of "troll"]

Well they sound the same in my more-or-less South London accent.
I can't write those funny phonetic symbols (and I hate to
imagine the Unicode encoding hoops I'd have to jump through
to make them readable anyway) but both "o"s sound short to me.
Like "bot" rather than "boat" using your example.
Since we're talking... I'm still a little startled when I listen
to some of the excellent webcasts that are being produced these
days (showmedo.com and friends) and hear American voices pronounce
Python... well, the way they do, with the stress and something of a
drawl on the second syllable. I'm sure it's just as amusing the other
way round: we pronounce it with the stress on the first syllable and
the characteristic short vowel sound in the second.
(Something like: Pie'thun).

TJG
Aug 31 '07 #55

[Carsten]
.................. If we start labeling
people, this thread will earn you a label that rhymes with "roll".
[Hendrik]
weird this - maybe a native English speaker can comment -
when I pronounce what fishermen do - it rhymes with roll,
but when I am talking about the thing that lives under bridges
and munches goats, the "O" sound is shorter, and more
towards the back of my mouth.
But - the word for someone who posts to the internet with the intention of
stirring up trouble derives from the word for what fishermen do, not from
the word for something that lives under a bridge. It derives from "trolling
for suckers" or "trolling for newbies".

--
Richie Hindle
ri****@entrian.com
Aug 31 '07 #56
On 8/30/07, Carsten Haese <ca*****@uniqsys.comwrote:
Is the Pythonic way

try:
i = somelist.index(thing)
# Do something with i
except IndexError:
# Do something if thing not found
That is not the Pythonic way. "# Do something with i" might also raise
an IndexError and they you are screwed. The Pythonic way is something
like:

try:
i = somelist.index(thing)
except IndexError:
print "Oh noes!"
else:
# Do the thing with i

And for many cases this actually is worse/less readable than the
alternative would have been if list.index() returned -1.
--
mvh BjŲrn
Aug 31 '07 #57
On Aug 31, 11:05 am, Jon Ribbens <jon+use...@unequivocal.co.ukwrote:
On 2007-08-31, Erik Max Francis <m...@alcyone.comwrote:
I say the 'oll' in troll like the 'ol' in frolic, and pronounce roll
and role similarly.
My accent is probably from the East Midlands of the UK, but is not
pronounced.
_Troll_ and _frolic_ aren't pronounced with the same "o" sound in any
accent I've ever heard of.

Welcome to England!
Which you pronounce _boat_ and _bot_ the same way, too?

No. HTH HAND.
For some, "troll" rhymes with "roll", for others, with "doll". Does it
matter?

As for the pronunciation of "Python", let's ask Guido! :-)

Aug 31 '07 #58
Paddy wrote:
On Aug 31, 8:47 am, Erik Max Francis <m...@alcyone.comwrote:
>Hendrik van Rooyen wrote:
>>weird this - maybe a native English speaker can comment -
when I pronounce what fishermen do - it rhymes with roll,
but when I am talking about the thing that lives under bridges
and munches goats, the "O" sound is shorter, and more
towards the back of my mouth.
Native English accents vary as well, but _roll_ rhymes with _troll_, not
_trawl_. _Trawl_ would rhyme with _fall_, and _fall_ definitely doesn't
rhyme with _roll_.

--
Erik Max Francis && m...@alcyone.com &&http://www.alcyone.com/max/
San Jose, CA, USA && 37 20 N 121 53 W && AIM, Y!M erikmaxfrancis
I do not like work even when someone else does it.
-- Mark Twain

I say the 'oll' in troll like the 'ol' in frolic, and pronounce roll
and role similarly.

My accent is probably from the East Midlands of the UK, but is not
pronounced.
If your accent isn't pronounced how do we know what it sounds like?

regards
Steve
--
Steve Holden +1 571 484 6266 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Skype: holdenweb http://del.icio.us/steve.holden
--------------- Asciimercial ------------------
Get on the web: Blog, lens and tag the Internet
Many services currently offer free registration
----------- Thank You for Reading -------------

Aug 31 '07 #59
On Fri, 31 Aug 2007 02:37:15 -0700, Erik Max Francis <ma*@alcyone.com>
wrote:
>_Troll_ and _frolic_ aren't pronounced with the same "o" sound in any
accent I've ever heard of.
You've never heard an English accent then.
>Which you pronounce _boat_ and _bot_ the same way, too?
No - but I would pronounce "lever" and "fever" the same way, if that helps.

DaveM
Aug 31 '07 #60
On Aug 31, 11:19 am, Tim Golden <m...@timgolden.me.ukwrote:
Tim Golden wrote:
Erik Max Francis wrote:
Paddy wrote:
>I say the 'oll' in troll like the 'ol' in frolic, and pronounce roll
and role similarly.
>My accent is probably from the East Midlands of the UK, but is not
pronounced.
_Troll_ and _frolic_ aren't pronounced with the same "o" sound in any
accent I've ever heard of. Which you pronounce _boat_ and _bot_ the
same way, too?
[Amusingly contemplating a trolling war about the pronunciation of "troll"]
Well they sound the same in my more-or-less South London accent.
I can't write those funny phonetic symbols (and I hate to
imagine the Unicode encoding hoops I'd have to jump through
to make them readable anyway) but both "o"s sound short to me.
Like "bot" rather than "boat" using your example.

Since we're talking... I'm still a little startled when I listen
to some of the excellent webcasts that are being produced these
days (showmedo.com and friends) and hear American voices pronounce
Python... well, the way they do, with the stress and something of a
drawl on the second syllable. I'm sure it's just as amusing the other
way round: we pronounce it with the stress on the first syllable and
the characteristic short vowel sound in the second.
(Something like: Pie'thun).

TJG
The only true way of pronouncing Python (the computing language), is
the way it is done at the beginning of Monty Pythons Flying Circus of
course :-)

Your right, the American way does make me pause.

- Paddy.

Aug 31 '07 #61
In message <ma**************************************@python.o rg>, Carsten
Haese wrote:
On Thu, 2007-08-30 at 11:21 -0700, zz******@aol.com wrote:
>I wish they were not
getting rid of dict.has_key() in Python 3, which I prefer to IN.

That wish will only come true if you maintain your own fork of Python 3.
has_key() will go away, period. It has been made obsolete by "in", which
is faster and more concise.
And is also a backdoor way of introducing non-virtual methods into Python,
is it not.
Sep 1 '07 #62
On Sat, 2007-09-01 at 13:50 +1200, Lawrence D'Oliveiro wrote:
In message <ma**************************************@python.o rg>, Carsten
Haese wrote:
has_key() will go away, period. It has been made obsolete by "in", which
is faster and more concise.

And is also a backdoor way of introducing non-virtual methods into Python,
is it not.
If by that you mean that "in" tests can't be overridden, that's not
true:
>>class LyingDict(dict):
.... def __contains__(self, key): return False
....
>>d = LyingDict()
d[1] = 42
1 in d
False

If you mean something else, please clarify.

--
Carsten Haese
http://informixdb.sourceforge.net
Sep 1 '07 #63
"Erik Max Francis" <ma...e.comwrote:

Hendrik van Rooyen wrote:
weird this - maybe a native English speaker can comment -
when I pronounce what fishermen do - it rhymes with roll,
but when I am talking about the thing that lives under bridges
and munches goats, the "O" sound is shorter, and more
towards the back of my mouth.

Native English accents vary as well, but _roll_ rhymes with _troll_, not
_trawl_. _Trawl_ would rhyme with _fall_, and _fall_ definitely doesn't
rhyme with _roll_.
I did not mean using a net to scour the seabed - Trawl,
I meant using a spoon to see what you can induce to strike - Troll

- Hendrik

Sep 1 '07 #64
Dennis Lee Bieber wrote:
On Fri, 31 Aug 2007 21:15:10 +0100, DaveM <as****@dsl.pipex.com>
declaimed the following in comp.lang.python:

>No - but I would pronounce "lever" and "fever" the same way, if that helps.
<shudderTo me, those are different... I suppose you also add an
extra "i" to aluminum <G>
No, he just spells it with two i's like sensible people do. Who would
ever want *three* I's in "aluminium" (which, by the way, you misspelled ;-)?

Such things are matters of practical significance to me, since I have
adopted a policy of using English spelling when in the UK and US
spelling when elsewhere. Since I am now pretty much full-time in the US,
I am finally having to come to terms with its crazy spelling.

regards
Steve
--
Steve Holden +1 571 484 6266 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Skype: holdenweb http://del.icio.us/steve.holden
--------------- Asciimercial ------------------
Get on the web: Blog, lens and tag the Internet
Many services currently offer free registration
----------- Thank You for Reading -------------

Sep 1 '07 #65
"Paddy" <pad....o.mail.comwrote:
>
I say the 'oll' in troll like the 'ol' in frolic, and pronounce roll
and role similarly.

My accent is probably from the East Midlands of the UK, but is not
pronounced.
Same here - when the Troll lives under a bridge - I could not think
of something to rhyme with it - frolic is just right.

- Hendrik

Sep 1 '07 #66
"Richie Hindle" <richi...ian.comwrote:
But - the word for someone who posts to the internet with the intention of
stirring up trouble derives from the word for what fishermen do, not from
the word for something that lives under a bridge. It derives from "trolling
for suckers" or "trolling for newbies".
So am I right in asserting that there is a difference in pronunciation
of the noun and the verb?

He is a Troll - like the excellent frolic example
He likes to Troll - rhymes with roll?

- Hendrik

Sep 1 '07 #67
Hendrik van Rooyen wrote:
"Richie Hindle" <richi...ian.comwrote:
>But - the word for someone who posts to the internet with the intention of
stirring up trouble derives from the word for what fishermen do, not from
the word for something that lives under a bridge. It derives from "trolling
for suckers" or "trolling for newbies".

So am I right in asserting that there is a difference in pronunciation
of the noun and the verb?

He is a Troll - like the excellent frolic example
He likes to Troll - rhymes with roll?
I think you are seeking consistency where none exists. I don't believe
anyone uses different pronunciations for the noun and the verb (which I
guess will make those who *do* come out of the woodwork in double-quick
time).

Where's Godwin's Law when yo need it?

regards
Steve
--
Steve Holden +1 571 484 6266 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Skype: holdenweb http://del.icio.us/steve.holden
--------------- Asciimercial ------------------
Get on the web: Blog, lens and tag the Internet
Many services currently offer free registration
----------- Thank You for Reading -------------

Sep 1 '07 #68
On Sep 1, 7:40 am, "Hendrik van Rooyen" <m...@microcorp.co.zawrote:
"Paddy" <pad....o.mail.comwrote:
I say the 'oll' in troll like the 'ol' in frolic, and pronounce roll
and role similarly.

Same here - when the Troll lives under a bridge - I could not think
of something to rhyme with it - frolic is just right.

- Hendrik
Yep, they would have posted about it way-back if they had the tech...

http://uk.youtube.com/watch?v=zZ3fjQa5Hls

(Gershwin doesn't seem to have a line about troll though :-)

- Paddy.
Sep 1 '07 #69
Dennis Lee Bieber <wl*****@ix.netcom.comwrites:
I suppose you also add an extra "i" to aluminum <G>
We're not out to rewrite the table of elements. There's no such thing
as "aluminum", and "aluminium" always has just the two "i"s.

--
\ "Rightful liberty is unobstructed action, according to our |
`\ will, within limits drawn around us by the equal rights of |
_o__) others." -- Thomas Jefferson |
Ben Finney
Sep 1 '07 #70
On Sep 1, 7:57 am, "Hendrik van Rooyen" <m...@microcorp.co.zawrote:
"Richie Hindle" <richi...ian.comwrote:
But - the word for someone who posts to the internet with the intention of
stirring up trouble derives from the word for what fishermen do, not from
the word for something that lives under a bridge. It derives from "trolling
for suckers" or "trolling for newbies".

So am I right in asserting that there is a difference in pronunciation
of the noun and the verb?

He is a Troll - like the excellent frolic example
He likes to Troll - rhymes with roll?

- Hendrik
No difference. A troll is a troll is a troll.

:-)

- Paddy.
Sep 1 '07 #71
On Sep 1, 7:32 am, Steve Holden <st...@holdenweb.comwrote:
Dennis Lee Bieber wrote:
On Fri, 31 Aug 2007 21:15:10 +0100, DaveM <asm...@dsl.pipex.com>
declaimed the following in comp.lang.python:
No - but I would pronounce "lever" and "fever" the same way, if that helps.
<shudderTo me, those are different... I suppose you also add an
extra "i" to aluminum <G>

No, he just spells it with two i's like sensible people do. Who would
ever want *three* I's in "aluminium" (which, by the way, you misspelled ;-)?

Such things are matters of practical significance to me, since I have
adopted a policy of using English spelling when in the UK and US
spelling when elsewhere. Since I am now pretty much full-time in the US,
I am finally having to come to terms with its crazy spelling.

regards
Steve
--
Steve Holden +1 571 484 6266 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Skype: holdenweb http://del.icio.us/steve.holden
--------------- Asciimercial ------------------
Get on the web: Blog, lens and tag the Internet
Many services currently offer free registration
----------- Thank You for Reading -------------
Since the language that inspired the programming language is most
definitely English as spoken in the UK, could it be that the highest
levels of Pythonistic Nirvana are to be reached only by ....
Nah - can't do it! (just spell colour right and I'll be OK).

- Paddy.

Sep 1 '07 #72
On Sat, 01 Sep 2007 17:23:04 +1000, Ben Finney wrote:
Dennis Lee Bieber <wl*****@ix.netcom.comwrites:
>I suppose you also add an extra "i" to aluminum <G>

We're not out to rewrite the table of elements. There's no such thing as
"aluminum", and "aluminium" always has just the two "i"s.
Although aluminium is the one and only correct spelling *grins*, it is
not actually true that it has always been spelt that way (or for you
Merkins out there, spelled). The discoverer of aluminium, Sir Humphrey
Davy, originally spelt it alumium, then aluminum, and finally
standardized on aluminium.

http://www.worldwidewords.org/articles/aluminium.htm
--
Steven.
Sep 1 '07 #73
"Steve Holden" <st***@holdenweb.comwrote:

Paddy wrote:
My accent is probably from the East Midlands of the UK, but is not
pronounced.
If your accent isn't pronounced how do we know what it sounds like?
When he says pronounced, he doesn't mean pronounced, he means pronounced!

- To shamelessly paraphrase the Master - (Plum of course)

; - ) - Hendrik

Sep 1 '07 #74
"Steve Holden" <s...web.comwrote:

Where's Godwin's Law when yo need it?
Hitler would not have spellt "you" like that...

- Hendrik

Sep 1 '07 #75
Paddy wrote:
On Sep 1, 7:57 am, "Hendrik van Rooyen" <m...@microcorp.co.zawrote:
>"Richie Hindle" <richi...ian.comwrote:
>>But - the word for someone who posts to the internet with the intention of
stirring up trouble derives from the word for what fishermen do, not from
the word for something that lives under a bridge. It derives from "trolling
for suckers" or "trolling for newbies".
So am I right in asserting that there is a difference in pronunciation
of the noun and the verb?

He is a Troll - like the excellent frolic example
He likes to Troll - rhymes with roll?

- Hendrik

No difference. A troll is a troll is a troll.

:-)

- Paddy.

BTW people , the word for what fishermen do is T R A W L and not troll
(Ha! and I'm not a native English speaker).
Sep 1 '07 #76
Ricardo ArŠoz wrote:
Paddy wrote:
>On Sep 1, 7:57 am, "Hendrik van Rooyen" <m...@microcorp.co.zawrote:
>>"Richie Hindle" <richi...ian.comwrote:
But - the word for someone who posts to the internet with the intention of
stirring up trouble derives from the word for what fishermen do, not from
the word for something that lives under a bridge. It derives from "trolling
for suckers" or "trolling for newbies".
So am I right in asserting that there is a difference in pronunciation
of the noun and the verb?

He is a Troll - like the excellent frolic example
He likes to Troll - rhymes with roll?

- Hendrik
No difference. A troll is a troll is a troll.

:-)

- Paddy.


BTW people , the word for what fishermen do is T R A W L and not troll
(Ha! and I'm not a native English speaker).
Just read the whole thread, or use a dictionary: in fishing, trolling
and trawling are two different things; the first is done with a net, the
second with a line.

regards
Steve
--
Steve Holden +1 571 484 6266 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Skype: holdenweb http://del.icio.us/steve.holden
--------------- Asciimercial ------------------
Get on the web: Blog, lens and tag the Internet
Many services currently offer free registration
----------- Thank You for Reading -------------

Sep 1 '07 #77
Steve Holden wrote:
Ricardo ArŠoz wrote:
>Paddy wrote:
>>On Sep 1, 7:57 am, "Hendrik van Rooyen" <m...@microcorp.co.zawrote:
"Richie Hindle" <richi...ian.comwrote:
But - the word for someone who posts to the internet with the intention of
stirring up trouble derives from the word for what fishermen do, not from
the word for something that lives under a bridge. It derives from "trolling
for suckers" or "trolling for newbies".
So am I right in asserting that there is a difference in pronunciation
of the noun and the verb?

He is a Troll - like the excellent frolic example
He likes to Troll - rhymes with roll?

- Hendrik
No difference. A troll is a troll is a troll.

:-)

- Paddy.

BTW people , the word for what fishermen do is T R A W L and not troll
(Ha! and I'm not a native English speaker).

Just read the whole thread, or use a dictionary: in fishing, trolling
and trawling are two different things; the first is done with a net, the
second with a line.

regards
Steve
Damn Wikipedia! It always gets things upside down, specially when I
'read the whole thread' :

"Trolling for fish" is a form of angling where lines with hook-rigged
lures are dragged behind a boat to entice fish to bite. Compare the term
"Trawling for fish," which involves dragging a net behind a boat to
catch large numbers of fish.

;c)

(Don't mind me. Just trolling...)


Sep 1 '07 #78
On Aug 30, 7:00 pm, Steve Holden <st...@holdenweb.comwrote:
You can also generate the files that are in one directory but ot the
other with

(afiles | bfiles) - (afiles & bfiles)
Or just (afiles ^ bfiles).

Nicko

--
(lambda f: lambda *a:f(f,*a))(
lambda f,l,i:l[i][1]+f(f,l,l[i][0]) if l[i][0]>0 else "")(
sorted(enumerate('~ooiirnncc@-kk.og'),key=lambda a:a[1]),0)[1:]

Sep 1 '07 #79
zz******@aol.com wrote:
In my case of have done os.listdir() on two directories. I want to see
what files are in directory A that are not in directory B.
I have used exceptions in other languages and only do so on logic that
should never happen. In this case it is known that some of the files
will not be in both lists. I just want to know which ones.
What's wrong, then, with doing:

if i in list:
print list.index(i)

Since if, as you proposed, list.index() returned some value to represent
"not found", you'd require an if anyway.

This is probably not pythonic, but at least it would overcome your
exceptions excuse.

If we were to program this .index() method in some language that
enforces contracts, like haskell, then we'd say that .index() expects a
value that exists in the list. So if you violate the contract, why
should you expect to *not* get an exception. Doing it any other way,
though, makes the code a lot more error prone.

Sep 1 '07 #80
Alex Martelli wrote:
is the "one obvious way to do it" (the set(...) is just a simple and
powerful optimization -- checking membership in a set is roughly O(1),
while checking membership in a list of N items is O(N)...).
Depending on a how a set is stored, I'd estimate any membership check in
a set to be O(log N). That's if it's stored in a tree of some kind,
which you'd need to fast finding. Say a balanced binary tree. Worst
case, you'd have to search half of the elements to find what you were
looking for.
>

Alex
Sep 1 '07 #81
On Sat, 01 Sep 2007 13:44:28 -0600, Michael L Torrie wrote:
Alex Martelli wrote:
>is the "one obvious way to do it" (the set(...) is just a simple and
powerful optimization -- checking membership in a set is roughly O(1),
while checking membership in a list of N items is O(N)...).

Depending on a how a set is stored, I'd estimate any membership check in
a set to be O(log N).
Sets are stored as hash tables so membership check is O(1) just like Alex
said.

Ciao,
Marc 'BlackJack' Rintsch
Sep 1 '07 #82
On Sat, 01 Sep 2007 13:37:29 -0600, Michael L Torrie wrote:
What's wrong, then, with doing:

if i in list:
print list.index(i)
If `i` is in the list this does the linear lookup twice.
If we were to program this .index() method in some language that
enforces contracts, like haskell, then we'd say that .index() expects a
value that exists in the list. So if you violate the contract, why
should you expect to *not* get an exception. Doing it any other way,
though, makes the code a lot more error prone.
Does Haskell exceptions? In Haskell I would expect such a function to
return the `Maybe` type.

Ciao,
Marc 'BlackJack' Rintsch
Sep 1 '07 #83
Marc 'BlackJack' Rintsch <bj****@gmx.netwrote:
On Sat, 01 Sep 2007 13:44:28 -0600, Michael L Torrie wrote:
Alex Martelli wrote:
is the "one obvious way to do it" (the set(...) is just a simple and
powerful optimization -- checking membership in a set is roughly O(1),
while checking membership in a list of N items is O(N)...).
Depending on a how a set is stored, I'd estimate any membership check in
a set to be O(log N).

Sets are stored as hash tables so membership check is O(1) just like Alex
said.
"Roughly" O(1), as I said, because of the usual issues with cost of
hashing, potential hashing conflicts, re-hashing (which requires
thinking in terms of *amortized* big-O, just like, say, list appends!),
etc, just like for any hash table implementation (though Python's, long
used and finely tuned in dicts then adopted for sets, is an exceedingly
good implementation, it IS possible to artificially construct a "worst
case" -- e.g., set(23+sys.maxint*i*2+i for i in xrange(24,199))...)
Alex
Sep 2 '07 #84
On Aug 30, 11:23 am, zzbba...@aol.com wrote:
Neil, Steve,

Thanks for the responses on sets. I have not used them before and was
not even aware Python had them. I will try them out.
And if there weren't sets you would still not use find or index but a
brute force method or dictionaries
for each in dir_a_list :
if each not in dir_b_list :
print each, "not in dir_b"

Sep 2 '07 #85
* Terry Reedy (Fri, 31 Aug 2007 02:28:36 -0400)
"Marc 'BlackJack' Rintsch" <bj****@gmx.netwrote in message
news:5j***********@mid.uni-berlin.de...
| On Thu, 30 Aug 2007 17:09:36 +1000, Ben Finney wrote:
| zz******@aol.com writes:
| >What's with the index() function of lists throwing an exception on not
| >found?
| >
| It's letting you know that the item isn't in the list. There's no
| sensible return value from an "index" function in that condition.
|
| What about -1? C programmers do this all the time. :-)

Because they do not have exceptions.
They have plenty of - but they can't catch them :)

Thorsten
Sep 3 '07 #86
* Ben Finney (Thu, 30 Aug 2007 18:02:15 +1000)
Bruno Desthuilliers <br********************@wtf.websiteburo.oops.comwr ites:
What's with using your brain instead of whining ?

Now now, no need for snappiness.
Actually there was. The OP's claim
| There are a million situations where you can have an item not be in
| a list and it is not an exception situation.

....is just plain nonsense. zzbbaadd neither does understand exceptions
nor what they are used for in Python. An item not being in a list is
an exception situation /by definition/.

Thorsten
Sep 3 '07 #87
>
Actually there was. The OP's claim
| There are a million situations where you can have an item not be in
| a list and it is not an exception situation.

...is just plain nonsense. zzbbaadd neither does understand exceptions
nor what they are used for in Python. An item not being in a list is
an exception situation /by definition/.
It won't be /by definition/ in my fork of Python 3, which I am pleased
to announce now, is called Python 3.01 while in development, and will
be known as Python 3000 or Python 3K when it gets to a productional
release. So in regard to Python 3000, your statement will be plain
nonsense.

Sep 4 '07 #88
On Mon, 03 Sep 2007 19:56:04 -0700, TheFlyingDutchman wrote
[...] my fork of Python 3, which I am
pleased to announce now, is called Python 3.01 while in development,
and will be known as Python 3000 or Python 3K when it gets to a productional
release.
I hope you're joking.

-Carsten

Sep 4 '07 #89
On Aug 30, 1:27 am, Marc 'BlackJack' Rintsch <bj_...@gmx.netwrote:
On Thu, 30 Aug 2007 17:09:36 +1000, Ben Finney wrote:
zzbba...@aol.com writes:
What's with the index() function of lists throwing an exception on not
found?
It's letting you know that the item isn't in the list. There's no
sensible return value from an "index" function in that condition.

What about -1? C programmers do this all the time. :-)

Ciao,
Marc 'BlackJack' Rintsch
As other people pointed out, C doesn't have exceptions, so a C
programmer must make an in/out parameter to indicate an error, or have
a special return value. In Python, you're most often searching the
list for an object which is in the list, so the lack of the object is
an exceptional condition. You can certain check with the "in"
operator beforehand to avoid the exception. You may also subclass a
list and override the index method, or write a standalone function to
catch the exception and change its value.

The reason why the exception is more Pythonic is that the return value
is always a guaranteed good index into the list. Any errors
(including calling .index() on a non-list instance that doesn't have
a .index method) are exceptional, and should probably follow a very
different code path.

Returning -1 is not a good return value to indicate an error. After
all, -1 is a valid index in most Python lists. (Negative numbers
index from the tail of the list.)

--Jason

Sep 4 '07 #90
Jason wrote:
On Aug 30, 1:27 am, Marc 'BlackJack' Rintsch <bj_...@gmx.netwrote:
>On Thu, 30 Aug 2007 17:09:36 +1000, Ben Finney wrote:
>>zzbba...@aol.com writes:
What's with the index() function of lists throwing an exception on not
found?
It's letting you know that the item isn't in the list. There's no
sensible return value from an "index" function in that condition.
What about -1? C programmers do this all the time. :-)

Ciao,
Marc 'BlackJack' Rintsch

As other people pointed out, C doesn't have exceptions, so a C
programmer must make an in/out parameter to indicate an error, or have
a special return value. In Python, you're most often searching the
list for an object which is in the list, so the lack of the object is
an exceptional condition. You can certain check with the "in"
operator beforehand to avoid the exception. You may also subclass a
list and override the index method, or write a standalone function to
catch the exception and change its value.

The reason why the exception is more Pythonic is that the return value
is always a guaranteed good index into the list. Any errors
(including calling .index() on a non-list instance that doesn't have
a .index method) are exceptional, and should probably follow a very
different code path.

Returning -1 is not a good return value to indicate an error. After
all, -1 is a valid index in most Python lists. (Negative numbers
index from the tail of the list.)

--Jason
Agree in general tho its a bit inconsistent how...

"string".find(val)

....can return -1 but .index() cant, though the inconsistency is probably
with string since most other areas of the py api raise errors in cases
like this.
Sep 4 '07 #91
On 2007-08-31, Paddy <pa*******@googlemail.comwrote:
On Aug 31, 11:19 am, Tim Golden <m...@timgolden.me.ukwrote:
>Tim Golden wrote:
Erik Max Francis wrote:
Paddy wrote:
>>I say the 'oll' in troll like the 'ol' in frolic, and pronounce roll
and role similarly.
>>My accent is probably from the East Midlands of the UK, but is not
pronounced.
_Troll_ and _frolic_ aren't pronounced with the same "o" sound in any
accent I've ever heard of. Which you pronounce _boat_ and _bot_ the
same way, too?
[Amusingly contemplating a trolling war about the pronunciation of "troll"]
Well they sound the same in my more-or-less South London accent.
I can't write those funny phonetic symbols (and I hate to
imagine the Unicode encoding hoops I'd have to jump through
to make them readable anyway) but both "o"s sound short to me.
Like "bot" rather than "boat" using your example.

Since we're talking... I'm still a little startled when I listen
to some of the excellent webcasts that are being produced these
days (showmedo.com and friends) and hear American voices pronounce
Python... well, the way they do, with the stress and something of a
drawl on the second syllable. I'm sure it's just as amusing the other
way round: we pronounce it with the stress on the first syllable and
the characteristic short vowel sound in the second.
(Something like: Pie'thun).

TJG

The only true way of pronouncing Python (the computing language), is
the way it is done at the beginning of Monty Pythons Flying Circus of
course :-)

Your right, the American way does make me pause.
Ya'll can keep yer gall-dern schwa outta my Pie-thawn, ya hear?

--
Neil Cerutti
Persons are prohibited from picking flowers from any but their own graves.
--sign at Pennsylvania cemetery
Sep 4 '07 #92
On 2007-09-04, Campbell Barton <cb*****@metavr.comwrote:
Jason wrote:
>Returning -1 is not a good return value to indicate an error.
After all, -1 is a valid index in most Python lists.
(Negative numbers index from the tail of the list.)

Agree in general tho its a bit inconsistent how...

"string".find(val)

...can return -1 but .index() cant, though the inconsistency is
probably with string since most other areas of the py api raise
errors in cases like this.
The two functions have different ranges, but also different
domains.

sequence.index
domain: any item '==' to an item in sequence
range: All non-negative indexes of sequence

string.find
domain: any string
range: -1 for not found, and all non-negative indexes of string.

If you want to search for subsequences in a sequence, a la
string.find, you can use something like (what I naively assume
will be a maximally efficent):

def find(seq, subseq):
""" Find a subsequence of seq in seq, and return its index. Return -1 if
subseq is not found.
>>seq = [0, 1, 2, 3, 4, 5]
find(seq, [0, 1, 2])
0
>>find(seq, [])
0
>>find(seq, [3, 4])
3
>>find(seq, [3, 2])
-1
>>find(seq, [5, 6])
-1
>>find(seq, [3])
3
>>find(seq, [0, 2])
-1

"""
i = 0
j = 0
while i < len(seq) and j < len(subseq):
if seq[i] == subseq[j]:
j += 1
else:
j = 0
i += 1
if j == len(subseq): return i - j
else: return -1

It's probable that a simpler implementation using slice
operations will be faster for shortish lengths of subseq. It was
certainly easier to get it working correctly. ;)

def find(seq, subseq):
for i, j in itertools.izip(xrange(len(seq)-len(subseq)),
xrange(len(subseq), len(seq))):
if subseq == seq[i:j]:
return i
return -1

--
Neil Cerutti
I pulled away from the side of the road, glanced at my mother-in-law and
headed over the embankment. --Insurance Claim Blooper
Sep 4 '07 #93
In message <11**********************@y42g2000hsy.googlegroups .com>, Jason
wrote:
The reason why the exception is more Pythonic is that the return value
is always a guaranteed good index into the list.
How do you explain dict.get, then?
Sep 5 '07 #94
Lawrence D'Oliveiro <ld*@geek-central.gen.new_zealandwrites:
In message <11**********************@y42g2000hsy.googlegroups .com>,
Jason wrote:
The reason why the exception is more Pythonic is that the return
value is always a guaranteed good index into the list.

How do you explain dict.get, then?
I explain it by noting that list.index and dict.get serve totally
different purposes. The former returns the index given a value; the
latter returns a value given a key.

There are many areas of poor symmetry in the language and libraries;
it isn't particularly clever or difficult to find them if one
looks. Doing so doesn't appear to illustrate any point I see relevant
in this thread.

--
\ "If you're a horse, and someone gets on you, and falls off, and |
`\ then gets right back on you, I think you should buck him off |
_o__) right away." -- Jack Handey |
Ben Finney
Sep 5 '07 #95
>
I explain it by noting that list.index and dict.get serve totally
different purposes. The former returns the index given a value; the
latter returns a value given a key.
And the former raises an exception if the value is not found, while
the latter returns None if the value is not found.
>
There are many areas of poor symmetry in the language and libraries;
it isn't particularly clever or difficult to find them if one
looks. Doing so doesn't appear to illustrate any point I see relevant
in this thread.
It has been stated in this thread, that in Python, exceptions are used
differently than in other languages. Rather than just use them for
catastrophic errors that can't be dealt with, Python raises exceptions
on routine and survivable situations such as an item not found in a
list. It was mentioned that an exception is more Pythonic than
returning a "not found" value.

Someone pointed out that find() in str was a contradiction to this
since it doesn't throw an exception but returns a "not found" value,
and the response was that it was believed that find() would be removed
from the language (if so, it does not currently show up on the
"removed list" as has_key() and a few others do -
http://docs.python.org/dev/3.0/whatsnew/3.0.html) and was only there
for "historical reasons".

I don't believe the poster was trying to illustrate a point already
mentioned in the thread, but to give a contradiction to statements in
the thread. He was asking the question - why doesn't dict.get() throw
an exception on not found if that is the Pythonic way?
Sep 5 '07 #96
TheFlyingDutchman wrote:
>I explain it by noting that list.index and dict.get serve totally
different purposes. The former returns the index given a value; the
latter returns a value given a key.

And the former raises an exception if the value is not found, while
the latter returns None if the value is not found.
>There are many areas of poor symmetry in the language and libraries;
it isn't particularly clever or difficult to find them if one
looks. Doing so doesn't appear to illustrate any point I see relevant
in this thread.

It has been stated in this thread, that in Python, exceptions are used
differently than in other languages. Rather than just use them for
catastrophic errors that can't be dealt with, Python raises exceptions
on routine and survivable situations such as an item not found in a
list. It was mentioned that an exception is more Pythonic than
returning a "not found" value.

Someone pointed out that find() in str was a contradiction to this
since it doesn't throw an exception but returns a "not found" value,
and the response was that it was believed that find() would be removed
from the language (if so, it does not currently show up on the
"removed list" as has_key() and a few others do -
http://docs.python.org/dev/3.0/whatsnew/3.0.html) and was only there
for "historical reasons".

I don't believe the poster was trying to illustrate a point already
mentioned in the thread, but to give a contradiction to statements in
the thread. He was asking the question - why doesn't dict.get() throw
an exception on not found if that is the Pythonic way?

Because it's an explicit short-cut for a frequently-required paradigm.
It's effectively an extension to dict indexing - which, you will recall,
*does* raise an exception when the key is not found. Does *everything*
have to raise such an exception?

There's no requirement for Python to be 100% consistent in its
application of a principle, but on the whole it is reasonably consistent.

For Pete's sake stop wasting your time and go do some programming.
Nothing will ever be perfect, not even Python ;-)

regards
Steve
--
Steve Holden +1 571 484 6266 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Skype: holdenweb http://del.icio.us/steve.holden
--------------- Asciimercial ------------------
Get on the web: Blog, lens and tag the Internet
Many services currently offer free registration
----------- Thank You for Reading -------------

Sep 5 '07 #97

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by questions? | last post: by
9 posts views Thread by William Meyer | last post: by
8 posts views Thread by sam | last post: by
35 posts views Thread by erikwickstrom | last post: by
2 posts views Thread by Georgy Panterov | last post: by
5 posts views Thread by Mr.SpOOn | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by Gurmeet2796 | last post: by
reply views Thread by mdpf | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.