473,320 Members | 2,052 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Algorithm used by keyword 'in'

#using python 2.4a3

say there is a list:

list = range(10000)

Now randomize it.

Next, command the interpreter:
465 in list True


What algorithm is used here?
-Derek.
Jul 18 '05 #1
8 1353

"Derek Rhodes" <rh****@worldpath.net> wrote in message
news:lY********************@twister.nyroc.rr.com.. .
#using python 2.4a3

say there is a list:

list = range(10000)

Now randomize it.

Next, command the interpreter:
465 in list True

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

-Derek.


Jul 18 '05 #2
On Sat, 09 Oct 2004 14:54:09 +0000, Derek Rhodes wrote:
What algorithm is used here?


In addition to John Roth's post, see the bottom of
http://docs.python.org/ref/sequence-types.html , which will help you
create your own algorithms for your own classes, if you like.
Jul 18 '05 #3
> In addition to John Roth's post, see the bottom of
http://docs.python.org/ref/sequence-types.html , which will help you
create your own algorithms for your own classes, if you like.


Thanks for quick response guys.

-Derek.
Jul 18 '05 #4
Derek Rhodes wrote:
In addition to John Roth's post, see the bottom of
http://docs.python.org/ref/sequence-types.html , which will help you
create your own algorithms for your own classes, if you like.


Thanks for quick response guys.

-Derek.

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

Jul 18 '05 #5
John Roth wrote:
.... 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
Sc***********@Acm.Org
Jul 18 '05 #6
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[i] < lst[i+1]

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

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

Jul 18 '05 #7
"Derek Rhodes" <rh****@worldpath.net> wrote in message news:<lY********************@twister.nyroc.rr.com> ...
#using python 2.4a3

say there is a list:

list = range(10000)

Now randomize it.

Next, command the interpreter:
465 in list True


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.

--
Chris Connett
Jul 18 '05 #8
dataangel <k0*****@kzoo.edu> writes:
Derek Rhodes wrote:
In addition to John Roth's post, see the bottom of
http://docs.python.org/ref/sequence-types.html , which will help you
create your own algorithms for your own classes, if you like.


Thanks for quick response guys.

-Derek.

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

--
The ability to quote is a serviceable substitute for wit.
-- W. Somerset Maugham
Jul 18 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

16
by: cody | last post by:
I have to write an algorithm with must ensure that objects are put in buckets (which are always 4 in size). The objects have two properties: A and B. It is not allowed that in a bucket are objects...
113
by: Bonj | last post by:
I was in need of an encryption algorithm to the following requirements: 1) Must be capable of encrypting strings to a byte array, and decyrpting back again to the same string 2) Must have the same...
2
by: Julio C. Hernandez Castro | last post by:
Dear all, We have just developped a new block cipher called Raiden, following a Feistel Network structure by means of genetic programming. Our intention now consists on getting as much feedback...
6
by: %NAME% | last post by:
The title is not exactly a disnomer, so pleaes let me explain... Say I have a large number of 0-1 vectors containing only bits corresponding to 0 or 1. Now given an arbitrary 0-1 vector, I wish...
6
by: pj | last post by:
Hi, I 'm currently writing a program that performs transliteration (i.e., converts greek text written using the english alphabet to "pure" greek text using the greek alphabet) as part of my...
1
by: sourab123 | last post by:
hello i got frustrated after making several tries to make the minimax algorithm work in my console tic tac toe. the problem is the algorithm doesn't play the game as it should.The thing is it just...
11
by: Dijkstra | last post by:
Hi folks! First, this is the code I'm using to expose the problem: ------------------------------------------------------------------ #include <functional> #include <string> #include...
23
by: raashid bhatt | last post by:
what is restrict keyword used for? eg int *restrict p;
2
by: shashankbs | last post by:
Given a topology and a certain node X, find the shortest path tree with X as the root. * Input: a topology file similar to the following (which is a three-node ring) ...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.