Algorithm used by keyword 'in'

D

Derek Rhodes

#using python 2.4a3

say there is a list:

list = range(10000)

Now randomize it.

Next, command the interpreter:

What algorithm is used here?


-Derek.
 
J

John Roth

Derek Rhodes said:
#using python 2.4a3

say there is a list:

list = range(10000)

Now randomize it.

Next, command the interpreter:


What algorithm is used here?

"in" asks the object to determine if the
operand is in the object. Since this is
a list, it's probably using a sequential
search. If it was a dictionary, it would
use a keyword lookup, as you would
expect. For any other object, it will
do whatever that object wants.

John Roth
 
J

Josiah Carlson

Is python smart enough to do say a binary search if you sort the list
right before using in?

No, use the bisect module if you want to binary search. Knowing that a
sequence is sorted requires keeping an 'is_sorted' boolean attached to
the object (which may end up increasing the space used by each list).

It is also ambiguous to say that a list is "sorted". Even if the
following passes without exception:

for i in xrange(len(lst)-1):
assert lst < lst[i+1]

That does not mean that there isn't an ambiguous ordering that would
make binary search and/or list.sort() incorrect:True

One should need to be explicit when they want to binary search, because
it may not be correct in some cases.

"In the face of ambiguity, refuse the temptation to guess."


- Josiah
 
S

Scott David Daniels

John said:
.... For any other object, it will do whatever that object wants.

Reminds me of one of my favorite technical one-liners:

Don't anthropomorphize computers.... They don't like that.

-Scott David Daniels
(e-mail address removed)
 
C

Chris Connett

Derek Rhodes said:
#using python 2.4a3

say there is a list:

list = range(10000)

Now randomize it.

Next, command the interpreter:


What algorithm is used here?

Since there are no guarantees about the list or the items it contains,
the best list can do is a linear search. Do be aware however that
each class can implement it's own behavior for keyword ``in``, by
implementing the ``__contains__`` method. For example, sets and dicts
use a hash table implementation for their __contains__ method, so
performance there will be O(1), but sets and dicts can only contain
hashable items. Your classes can implement it however they want, with
whatever semantics you want.
 
M

Michael Hudson

dataangel said:
Is python smart enough to do say a binary search if you sort the list
right before using in?

No. How would Python be able to tell the list was sorted? There's
the bisect module for when *you* know the list is sorted.

Cheers,
mwh
 

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,209
Messages
2,571,088
Members
47,684
Latest member
sparada

Latest Threads

Top