list index()

P

Paddy

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...


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

- Paddy.
 
B

Ben Finney

Dennis Lee Bieber said:
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.
 
P

Paddy

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.
 
P

Paddy

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.
 
H

Hendrik van Rooyen

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
 
S

Steven D'Aprano

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
 
?

=?ISO-8859-1?Q?Ricardo_Ar=E1oz?=

Paddy said:
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).
 
S

Steve Holden

Ricardo said:
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 -------------
 
?

=?ISO-8859-1?Q?Ricardo_Ar=E1oz?=

Steve said:
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...)
 
M

Michael L Torrie

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.
 
M

Michael L Torrie

Alex said:
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.
 
M

Marc 'BlackJack' Rintsch

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
 
M

Marc 'BlackJack' Rintsch

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
 
A

Alex Martelli

Marc 'BlackJack' Rintsch said:
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
 
Z

Zentrader

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"
 
T

Thorsten Kampe

* Terry Reedy (Fri, 31 Aug 2007 02:28:36 -0400)
| On Thu, 30 Aug 2007 17:09:36 +1000, Ben Finney wrote:
| > (e-mail address removed) 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
 
T

Thorsten Kampe

* Ben Finney (Thu, 30 Aug 2007 18:02:15 +1000)
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top