473,602 Members | 2,861 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1363

"Derek Rhodes" <rh****@worldpa th.net> wrote in message
news:lY******** ************@tw ister.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 anthropomorphiz e computers.... They don't like that.

-Scott David Daniels
Sc***********@A cm.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****@worldpa th.net> wrote in message news:<lY******* *************@t wister.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.e du> 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
2646
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 with the same A or B value. But there can be more than one object with A = null or B = null in the bucket. Sometimes there is only one valid solution, sometimes there are more valid solutions, and sometimes there isn't a complete solution at...
113
12280
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 algorithm work with strings that may or may not be unicode 3) Number of bytes back must either be <= number of _TCHARs in * sizeof(_TCHAR), or the relation between output size and input size can be calculated simply. Has to take into account the...
2
7279
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 as possible from users, so we encourage you to test the algorithm and send us your opinion. We would also like to receive enhancements and new versions of the algorithm, developed in other source languages and platforms. Our idea on developing...
6
1615
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 to find all the 0-1 vectors that are subsumed by that query bit vector. For example, given bit vector query as 01100, i wish to find all bit vectors that whose 1st, 4th, 5th bits are also zero, leaving the 2nd, 3rd bits as either 0 or 1.
6
4082
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 thesis. I have decided to add the capability to convert words using some sort of lookup algorithm as a sidekick to the "normal" conversion algorithm, and here it starts getting interesting. I want to find an algorithm that satisfies these...
1
2125
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 tries to play the move that it 'sees' to be the best for itself without caring that its opponet is building an easy three to win the game. For ex. it playing as second player as ist player row=0;col=0; minimax player row=2;col=1; ist player...
11
2274
by: Dijkstra | last post by:
Hi folks! First, this is the code I'm using to expose the problem: ------------------------------------------------------------------ #include <functional> #include <string> #include <iostream> using namespace std;
23
4810
by: raashid bhatt | last post by:
what is restrict keyword used for? eg int *restrict p;
2
4648
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) -------------------------------------------- Nodes: (3) // there are three nodes (node 0, 1 and 2) Edges: (3)
0
7993
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
7921
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8270
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6730
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5440
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
3901
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2424
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1511
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1256
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.