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. 96 4335
On Wed, 29 Aug 2007 23:44:33 -0700, zzbbaadd wrote:
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.
Write such a function yourself, it is quite easy after all. I very seldom
use the `list.index()` method. What do your millions situations look like?
Maybe there is a better data structure than lists for those situations!?
Ciao,
Marc 'BlackJack' Rintsch 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.
Let's hope this is rectified in Python 3. If nothing else, add a
function that doesn't throw an exception.
You can easily create one:
def get_an_index_even_if_not_found(the_list, the_item):
bogus_index_value = object()
try:
index = the_list.index(the_value)
except ValueError:
index = bogus_index_value
return index
It's up to you to figure out what bogus_index_value you want to
use. The rest of us will continue to catch the exception where needed.
--
\ "Reichel's Law: A body on vacation tends to remain on vacation |
`\ unless acted upon by an outside force." -- Carol Reichel |
_o__) |
Ben Finney
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. :-)
Ciao,
Marc 'BlackJack' Rintsch zz******@aol.com a écrit :
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.
What's with using your brain instead of whining ?
Marc 'BlackJack' Rintsch <bj****@gmx.netwrites:
On Thu, 30 Aug 2007 17:09:36 +1000, Ben Finney wrote:
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. :-)
I don't believe you've contradicted me :-)
--
\ "If you ever drop your keys into a river of molten lava, let |
`\ 'em go, because, man, they're gone." -- Jack Handey |
_o__) |
Ben Finney
Bruno Desthuilliers <br********************@wtf.websiteburo.oops.comwr ites:
What's with using your brain instead of whining ?
Now now, no need for snappiness. If you don't feel a constructive
response is merited, please ignore.
--
\ "A lot of people are afraid of heights. Not me, I'm afraid of |
`\ widths." -- Steven Wright |
_o__) |
Ben Finney
Ben Finney <bi****************@benfinney.id.auwrites:
def get_an_index_even_if_not_found(the_list, the_item):
Bah. Should be "…(the_list, the_value):".
--
\ "Don't worry about people stealing your ideas. If your ideas |
`\ are any good, you'll have to ram them down people's throats." |
_o__) -- Howard Aiken |
Ben Finney
Ben Finney a écrit :
Bruno Desthuilliers <br********************@wtf.websiteburo.oops.comwr ites:
>What's with using your brain instead of whining ?
Now now, no need for snappiness. If you don't feel a constructive
response is merited, please ignore.
Yes, you're right. Sorry.
On Wed, 2007-08-29 at 23:44 -0700, zz******@aol.com wrote:
What's with the index() function of lists throwing an exception on not
found? Let's hope this is rectified in Python 3.
You're assuming that this behavior is a mistake. It's not, and
consequently, it won't be "rectified".
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.
How could it not be an exception, in the plain English sense of the
word? Most certainly you're asking for the index because you want to do
something with the index. If the item is not found, you have no index,
so that's a special case that must be handled separately. There is no
logical difference between handling that special case in an except
clause versus handling it with an if-branch.
Is the Pythonic way
try:
i = somelist.index(thing)
# Do something with i
except IndexError:
# Do something if thing not found
really that much worse than the theoretical alternative
i = somelist.index_that_returns_an_indicator(thing)
if i!=ThingNotFoundIndicator:
# Do something with i
else:
# Do something if thing not found
?
--
Carsten Haese http://informixdb.sourceforge.net zz******@aol.com writes:
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.
The Python string types have both the method `index()` which throws an
exception and the method `find()` which implements the same behavior but
returns -1 for not-found substrings. I would naively have assumed the
`list` type to provide both as well, but it provides only `index()`.
Anyone know the reason for this lack of parallelism?
-Marshall
On Thu, 30 Aug 2007 09:53:53 -0400, Marshall T. Vandegrift wrote:
The Python string types have both the method `index()` which throws an
exception and the method `find()` which implements the same behavior but
returns -1 for not-found substrings. I would naively have assumed the
`list` type to provide both as well, but it provides only `index()`.
Anyone know the reason for this lack of parallelism?
Historical reasons and IIRC the `find()` method on strings will go away in
PythonÂ*3.0.
Ciao,
Marc 'BlackJack' Rintsch
On Aug 30, 12:09 am, Ben Finney <bignose+hates-s...@benfinney.id.au>
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.
for str:
find( sub[, start[, end]])
Return the lowest index in the string where substring sub is
found, such that sub is contained in the range [start, end]. Optional
arguments start and end are interpreted as in slice notation. Return
-1 if sub is not found.
-1 is used in other languages as well.
>
How could it not be an exception, in the plain English sense of the
word? Most certainly you're asking for the index because you want to do
something with the index. If the item is not found, you have no index,
so that's a special case that must be handled separately. There is no
logical difference between handling that special case in an except
clause versus handling it with an if-branch.
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.
On Aug 30, 12:42 am, Bruno Desthuilliers <bruno.
42.desthuilli...@wtf.websiteburo.oops.comwrote:
zzbba...@aol.com a écrit :
What's with using your brain instead of whining ?
I knew there would be at least one religious zealot. zz******@aol.com wrote:
>How could it not be an exception, in the plain English sense of the word? Most certainly you're asking for the index because you want to do something with the index. If the item is not found, you have no index, so that's a special case that must be handled separately. There is no logical difference between handling that special case in an except clause versus handling it with an if-branch.
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.
Python is different than those languages. Exceptions are used much more
frequently in Python and often for things that will *definitely* happen not just
those things that shouldn't.
--
Robert Kern
"I have come to believe that the whole world is an enigma, a harmless enigma
that is made terrible by our own mad attempt to interpret it as though it had
an underlying truth."
-- Umberto Eco zz******@aol.com wrote:
>How could it not be an exception, in the plain English sense of the word? Most certainly you're asking for the index because you want to do something with the index. If the item is not found, you have no index, so that's a special case that must be handled separately. There is no logical difference between handling that special case in an except clause versus handling it with an if-branch.
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.
And, as is so often the case, once the *real* problem is stated a more
elegant solution become available - in this case, using sets.
afiles = set(os.listdir(dira))
bfiles = set(os.listdir(dirb))
for f in (afiles - bfiles):
print "Not common:", f
You can also generate the files that are in one directory but ot the
other with
(afiles | bfiles) - (afiles & bfiles)
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 -------------
On Thu, 2007-08-30 at 10:44 -0700, zz******@aol.com wrote:
How could it not be an exception, in the plain English sense of the
word? Most certainly you're asking for the index because you want to do
something with the index. If the item is not found, you have no index,
so that's a special case that must be handled separately. There is no
logical difference between handling that special case in an except
clause versus handling it with an if-branch.
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.
[...]
list.index() is the wrong tool for that job. Python has sets, use them.
--
Carsten Haese http://informixdb.sourceforge.net
On Thu, 2007-08-30 at 10:56 -0700, zz******@aol.com wrote:
On Aug 30, 12:42 am, Bruno Desthuilliers <bruno.
42.desthuilli...@wtf.websiteburo.oops.comwrote:
zzbba...@aol.com a crit :
What's with using your brain instead of whining ?
I knew there would be at least one religious zealot.
While I agree that Bruno's response was perhaps needlessly snippy, your
original question was needlessly inflammatory, as if you somehow wanted
some "religious zealot" to act the way Bruno did. If we start labeling
people, this thread will earn you a label that rhymes with "roll".
--
Carsten Haese http://informixdb.sourceforge.net
On 2007-08-30, zz******@aol.com <zz******@aol.comwrote:
>How could it not be an exception, in the plain English sense of the word? Most certainly you're asking for the index because you want to do something with the index. If the item is not found, you have no index, so that's a special case that must be handled separately. There is no logical difference between handling that special case in an except clause versus handling it with an if-branch.
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.
In that case list.find would not be much of a win, but sets might
be.
not_in_both = list(set(os.listdir("A")) - set(os.listdir("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.
"Exceptions" has become somewhat a misnomer. iterators are
implemented using exceptions, and there's hardly anything more
common in modern Python code. The hair shirts and thumb-screws
necessary for using exceptions correctly in C++, aren't needed in
Python.
--
Neil Cerutti
While I agree that Bruno's response was perhaps needlessly snippy, your
original question was needlessly inflammatory, as if you somehow wanted
some "religious zealot" to act the way Bruno did. If we start labeling
people, this thread will earn you a label that rhymes with "roll".
That is correct. I did word it in a way that would hook the Bruno's of
the group. But I will probably have some more important questions so I
will word it differently in the future. Such as: I wish they were not
getting rid of dict.has_key() in Python 3, which I prefer to IN.
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.
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.
--
Carsten Haese http://informixdb.sourceforge.net zz******@aol.com wrote:
>While I agree that Bruno's response was perhaps needlessly snippy, your original question was needlessly inflammatory, as if you somehow wanted some "religious zealot" to act the way Bruno did. If we start labeling people, this thread will earn you a label that rhymes with "roll".
That is correct. I did word it in a way that would hook the Bruno's of
the group. But I will probably have some more important questions so I
will word it differently in the future. Such as: I wish they were not
getting rid of dict.has_key() in Python 3, which I prefer to IN.
Well, again Python gives you the flexibility to define your own dict
subclass that has a has_key() method with the obvious implementation.
But why would you want to ignore built-in support like "value in dict"?
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 -------------
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.
Is there really some reason "key" IN dict can be implemented faster
than dict.has_key("key")???
On 2007-08-30, zz******@aol.com <zz******@aol.comwrote:
>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.
Is there really some reason "key" IN dict can be implemented
faster than dict.has_key("key")???
Yes. Looking up the has_key method by name is the slower part.
--
Neil Cerutti
We're not afraid of challenges. It's like we always say: If you want to go out
in the rain, be prepared to get burned. --Brazillian soccer player
On 8/30/07, zz******@aol.com <zz******@aol.comwrote:
>
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.
Is there really some reason "key" IN dict can be implemented faster
than dict.has_key("key")???
Yes. As an exercise, I wont tell you the first most obvious reason -
see if you can figure it out. The dis module may help.
On Aug 30, 7:00 pm, Steve Holden <st...@holdenweb.comwrote:
zzbba...@aol.com wrote:
How could it not be an exception, in the plain English sense of the
word? Most certainly you're asking for the index because you want to do
something with the index. If the item is not found, you have no index,
so that's a special case that must be handled separately. There is no
logical difference between handling that special case in an except
clause versus handling it with an if-branch.
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.
And, as is so often the case, once the *real* problem is stated a more
elegant solution become available - in this case, using sets.
afiles = set(os.listdir(dira))
bfiles = set(os.listdir(dirb))
for f in (afiles - bfiles):
print "Not common:", f
You can also generate the files that are in one directory but ot the
other with
(afiles | bfiles) - (afiles & bfiles)
.... which can be written more concisely as:
afiles ^ bfiles
:-)
On Aug 30, 5:41 pm, zzbba...@aol.com wrote:
On Aug 30, 12:09 am, Ben Finney <bignose+hates-s...@benfinney.id.au>
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.
for str:
find( sub[, start[, end]])
Return the lowest index in the string where substring sub is
found, such that sub is contained in the range [start, end]. Optional
arguments start and end are interpreted as in slice notation. Return
-1 if sub is not found.
-1 is used in other languages as well.
In 0-based languages it's often -1 whereas in 1-based languages it's
often 0.
On Thu, 30 Aug 2007 09:41:00 -0700, zzbbaadd wrote:
On Aug 30, 12:09 am, Ben Finney <bignose+hates-s...@benfinney.id.au>
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.
for str:
find( sub[, start[, end]])
Return the lowest index in the string where substring sub is
found, such that sub is contained in the range [start, end]. Optional
arguments start and end are interpreted as in slice notation. Return
-1 if sub is not found.
But that is a valid index into a string! So this may go unnoticed if one
doesn't check after every call to `str.find()`. And that method is going
away in PythonÂ*3.0.
Ciao,
Marc 'BlackJack' Rintsch zz******@aol.com writes:
On Aug 30, 12:09 am, Ben Finney <bignose+hates-s...@benfinney.id.au>
wrote:
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.
for str:
find( sub[, start[, end]])
[...]
Return -1 if sub is not found.
-1 is used in other languages as well.
It is no more sensible there than in the 'str.find' method, which is a
historical wart.
--
\ "Hey Homer! You're late for English!" "Pff! English, who needs |
`\ that? I'm never going to England!" -- Barney & Homer, _The |
_o__) Simpsons_ |
Ben Finney zz******@aol.com writes:
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.
You get that information unambiguously. It's an exceptional case,
since there's no index to return, so it throws an exception.
I have used exceptions in other languages and only do so on logic
that should never happen.
You're confusing "assert" ("this should always be true") with
"exception" ("this is an exception to the the normal flow of this
process").
An exception isn't "something that should never happen", it's
something that is entirely possible and needs to be handled somehow.
--
\ "Always do right. This will gratify some people, and astonish |
`\ the rest." -- Mark Twain |
_o__) |
Ben Finney
On Aug 30, 4:28 pm, Ben Finney <bignose+hates-s...@benfinney.id.au>
wrote:
zzbba...@aol.com writes:
On Aug 30, 12:09 am, Ben Finney <bignose+hates-s...@benfinney.id.au>
wrote:
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.
for str:
find( sub[, start[, end]])
[...]
Return -1 if sub is not found.
-1 is used in other languages as well.
It is no more sensible there than in the 'str.find' method, which is a
historical wart.
One man's sensible is another man's insensible. For instance, some
people feel -1 as a valid index into a list is sensible. Other's find
it insensible.
>
--
\ "Hey Homer! You're late for English!" "Pff! English, who needs |
`\ that? I'm never going to England!" -- Barney & Homer, _The |
_o__) Simpsons_ |
Ben Finney
On Aug 30, 4:31 pm, Ben Finney <bignose+hates-s...@benfinney.id.au>
wrote:
zzbba...@aol.com writes:
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.
You get that information unambiguously. It's an exceptional case,
since there's no index to return, so it throws an exception.
I have used exceptions in other languages and only do so on logic
that should never happen.
You're confusing "assert" ("this should always be true") with
"exception" ("this is an exception to the the normal flow of this
process").
An exception isn't "something that should never happen", it's
something that is entirely possible and needs to be handled somehow.
I don't think that is the definition used across computer science.
It suddenly dawned on me that what would be best would be a contains()
(or IN syntax for those who can't afford to wait) for lists.
if mylist.contains("hello):
>
--
\ "Always do right. This will gratify some people, and astonish |
`\ the rest." -- Mark Twain |
_o__) |
Ben Finney
On 8/30/07, zz******@aol.com <zz******@aol.comwrote:
On Aug 30, 4:31 pm, Ben Finney <bignose+hates-s...@benfinney.id.au>
wrote:
zzbba...@aol.com writes:
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.
You get that information unambiguously. It's an exceptional case,
since there's no index to return, so it throws an exception.
I have used exceptions in other languages and only do so on logic
that should never happen.
You're confusing "assert" ("this should always be true") with
"exception" ("this is an exception to the the normal flow of this
process").
An exception isn't "something that should never happen", it's
something that is entirely possible and needs to be handled somehow.
I don't think that is the definition used across computer science.
how its defined in Python is what is important in this context.
It suddenly dawned on me that what would be best would be a contains()
(or IN syntax for those who can't afford to wait) for lists.
if mylist.contains("hello):
Genius. Why didn't anyone think of that before?
In message <ma**************************************@python.o rg>, Steve
Holden wrote:
But why would you want to ignore built-in support like "value in dict"?
Because a function can be passed as a value in its own right, a built-in
construct cannot.
On Thu, 2007-08-30 at 16:42 -0700, zz******@aol.com wrote:
It suddenly dawned on me that what would be best would be a contains()
(or IN syntax for those who can't afford to wait) for lists.
Either I'm misunderstanding what you mean or you need to get a clue
about what Python can already do before you go around making suggestions
for what Python needs. Lists have supported "in" tests since at least
version 1.5.2:
Python 1.5.2 (#1, Nov 6 1999, 14:53:40) [C] on sco_sv3
Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam
>>if 3 in [1,2,3]: print "Yup, got it!"
....
Yup, got it!
This feature may be older than version 1.5.2, but this is the oldest
version that I still have running anywhere.
--
Carsten Haese http://informixdb.sourceforge.net
On Fri, 2007-08-31 at 12:57 +1200, Lawrence D'Oliveiro wrote:
In message <ma**************************************@python.o rg>, Steve
Holden wrote:
But why would you want to ignore built-in support like "value in dict"?
Because a function can be passed as a value in its own right, a built-in
construct cannot.
....and that's why we have operator.contains():
>>import operator help(operator.contains)
Help on built-in function contains in module operator:
contains(...)
contains(a, b) -- Same as b in a (note reversed operands).
--
Carsten Haese http://informixdb.sourceforge.net
<zz******@aol.comwrote:
...
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.
So why would you care about WHERE, in the listdir of B, are to be found
the files that are in A but not B?! You should call .index only if you
CARE about the position.
def inAnotB(A, B):
inA = os.listdir(A)
inBs = set(os.listdir(B))
return [f for f in inA if f not in inBs]
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)...).
Alex
In message <87************@benfinney.id.au>, 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.
How about returning None? That's what's already done by the dict.get method
by default.
Either I'm misunderstanding what you mean or you need to get a clue
about what Python can already do before you go around making suggestions
for what Python needs. Lists have supported "in" tests since at least
version 1.5.2:
Well IN was what I was looking for and would have saved this thread.
However I don't believe IN showed up on the doc web page that has list
methods, where I found index().
On Aug 30, 9:26 pm, al...@mac.com (Alex Martelli) wrote:
<zzbba...@aol.comwrote:
...
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.
So why would you care about WHERE, in the listdir of B, are to be found
the files that are in A but not B?! You should call .index only if you
CARE about the position.
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)...).
Why wouldn't "the one obvious way" be:
def inAnotB(A, B):
inA = set(os.listdir(A))
inBs = set(os.listdir(B))
return inA.difference(inBs)
?
>
Alex
On Aug 30, 9:06 pm, "Carsten Haese" <cars...@uniqsys.comwrote:
On Thu, 30 Aug 2007 20:17:00 -0700, zzbbaadd wrote
Well IN was what I was looking for and would have saved this thread.
However I don't believe IN showed up on the doc web page that has
list methods, where I found index().
They're not on the exact same page, but index() is in section 3.6.4 of the
library reference (http://docs.python.org/lib/typesseq-mutable.html), whereas
"in" is in section 3.6 of the library reference
(http://docs.python.org/lib/typesseq.html). I'm wondering how you managed to
find subsection 3.6.4 without finding section 3.6.
www.google.com
search "python list methods"
first search find:
5. Data Structures
The list methods make it very easy to use a list as a stack, where the
last element added .... Another useful data type built into Python is
the dictionary. ... http://docs.python.org/tut/node7.html
"The list data type has some more methods. Here are all of the methods
of list objects:"
"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. zz******@aol.com a écrit :
On Aug 30, 4:28 pm, Ben Finney <bignose+hates-s...@benfinney.id.au>
wrote:
>zzbba...@aol.com writes:
>>On Aug 30, 12:09 am, Ben Finney <bignose+hates-s...@benfinney.id.au> wrote: 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. for str: find( sub[, start[, end]]) [...] Return -1 if sub is not found. -1 is used in other languages as well.
It is no more sensible there than in the 'str.find' method, which is a historical wart.
One man's sensible is another man's insensible. For instance, some
people feel -1 as a valid index into a list is sensible. Other's find
it insensible.
Python 2.5.1 (r251:54863, May 2 2007, 16:56:35)
[GCC 4.1.2 (Ubuntu 4.1.2-0ubuntu4)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>print "sensible"[-1]
e
>>>
"Carsten Haese" <car...ys.comwrote:
.................. If we start labeling
people, this thread will earn you a label that rhymes with "roll".
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.
- Hendrik
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 && ma*@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 zz******@aol.com a écrit :
(snip)
I don't think that is the definition used across computer science.
It suddenly dawned on me that what would be best would be a contains()
(or IN syntax for those who can't afford to wait) for lists.
No need to wait:
>>'a' in ['a', 'b']
True
>>['a', 'b'].__contains__('a')
True 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
| |
3 posts
views
Thread by Riccardo Murri |
last post: by
|
2 posts
views
Thread by Georgy Panterov |
last post: by
|
5 posts
views
Thread by Mr.SpOOn |
last post: by
| | | | | | | | | | | |