473,407 Members | 2,546 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,407 software developers and data experts.

Whither binary search?

I can't find the mystic search glob that will find a binary
search function in the Python documentation.

Am I searching in vain, or do I need better search-fu?

--
Neil Cerutti
Sep 26 '06 #1
8 1901
bisect...

Skip
Sep 26 '06 #2
On 2006-09-26, sk**@pobox.com <sk**@pobox.comwrote:
bisect...
That doesn't tell me if an item doesn't exist in the sequence
though, does it? Maybe I'm being dense.

My guess nobody keeps their sequences sorted in Python. ;)

--
Neil Cerutti
Sep 26 '06 #3
Neil Cerutti wrote:
>bisect...

That doesn't tell me if an item doesn't exist in the sequence
though, does it? Maybe I'm being dense.
I guess you could use something like

import bisect

def check(list, item):
try:
return list[bisect.bisect_left(list, item)] == item
except IndexError:
return False

but unless your lists are really large, or your objects are expensive to
compare, you could probably just use "in" instead.
My guess nobody keeps their sequences sorted in Python.
well, people tend to use dictionaries when they need to look things up
quickly...

</F>

Sep 26 '06 #4

NeilOn 2006-09-26, sk**@pobox.com <sk**@pobox.comwrote:
>bisect...
NeilThat doesn't tell me if an item doesn't exist in the sequence
Neilthough, does it? Maybe I'm being dense.

NeilMy guess nobody keeps their sequences sorted in Python. ;)

Sorry, I was on the phone as I replied, and was only typing with my left
hand, so my response was intentionally very brief. ;-)

If you use bisect to do your insertions, your lists remain sorted. And as
for searching, all you have to do is look to the left or the right of the
insertion point (depending on which search method you call) to determine if
the item you're searching for is in the list.

Skip
Sep 26 '06 #5

Fredrik Lundh wrote:
Neil Cerutti wrote:
bisect...
That doesn't tell me if an item doesn't exist in the sequence
though, does it? Maybe I'm being dense.

I guess you could use something like

import bisect

def check(list, item):
try:
return list[bisect.bisect_left(list, item)] == item
except IndexError:
return False

but unless your lists are really large, or your objects are expensive to
compare, you could probably just use "in" instead.
My guess nobody keeps their sequences sorted in Python.

well, people tend to use dictionaries when they need to look things up
quickly...
.... like those paper dictionaries with the words in alphabetical order
:-)

A common use case for looking things up in a sorted list is an
in-memory table of dates and rates (interest rates, insurance premium
rates, fee rates). In a big batch job, this sure beats doing "select
rate from table where ? between low_date and high_date" each time you
need a rate.

Cheers,
John

Sep 26 '06 #6
John Machin <sj******@lexicon.netwrote:
>Fredrik Lundh wrote:
>well, people tend to use dictionaries when they need to look things up
quickly...
... like those paper dictionaries with the words in alphabetical order
:-)
.... where you'll notice that the really big ones are divided up into
buckets (which just happen to be keyed on initial letter).

Truth is that humans are lot better than computers at general
insertion sort, and its lookup equivalent, whereas computers are much
better at calculating hashes. So we each use a dictionary
implementation that plays to our strengths.

--
\S -- si***@chiark.greenend.org.uk -- http://www.chaos.org.uk/~sion/
___ | "Frankly I have no feelings towards penguins one way or the other"
\X/ | -- Arthur C. Clarke
her nu becomež se bera eadward ofdun hlęddre heafdes bęce bump bump bump
Sep 28 '06 #7
On 2006-09-28, Sion Arrowsmith <si***@chiark.greenend.org.ukwrote:
John Machin <sj******@lexicon.netwrote:
>>Fredrik Lundh wrote:
>>well, people tend to use dictionaries when they need to look things up
quickly...
... like those paper dictionaries with the words in alphabetical order
:-)

... where you'll notice that the really big ones are divided up
into buckets (which just happen to be keyed on initial letter).

Truth is that humans are lot better than computers at general
insertion sort, and its lookup equivalent, whereas computers
are much better at calculating hashes. So we each use a
dictionary implementation that plays to our strengths.
Thanks all, for the help.

In the end, perhaps prophetically, it turned out my data wasn't
really fully sorted after all. I put together a short function to
translate my raw data into a dictionary, and that was the end of
my problem.

--
Neil Cerutti
Sep 28 '06 #8

Sion Arrowsmith wrote:
John Machin <sj******@lexicon.netwrote:
Fredrik Lundh wrote:
well, people tend to use dictionaries when they need to look things up
quickly...
... like those paper dictionaries with the words in alphabetical order
:-)

... where you'll notice that the really big ones are divided up into
buckets (which just happen to be keyed on initial letter).
And languages whose scripts don't have letters use other bucket keys
like pronunciation or (radical, stroke_count). In any case, the
buckets are printed in key order. The bucket index, if printed, is
itself sorted.
>
Truth is that humans are lot better than computers at general
insertion sort, and its lookup equivalent, whereas computers are much
better at calculating hashes. So we each use a dictionary
implementation that plays to our strengths.
Hashes are fine for simplistic applications.
| >>hash('initialise') == hash('initialize')
| False
but those two are not very far apart in a sorted list.

Cheers,
John

Sep 28 '06 #9

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

Similar topics

5
by: Ravi | last post by:
I recently had to write some code which required me to use a binary search tree (BST) due to running time concerns. I found that there is no BST class in the Standard C++ library. I have been told...
4
by: Tarique Jawed | last post by:
Alright I needed some help regarding a removal of a binary search tree. Yes its for a class, and yes I have tried working on it on my own, so no patronizing please. I have most of the code working,...
9
by: Hemang Shah | last post by:
Hello fellow Coders! ok, I"m trying to write a very simple application in C#. (Yes its my first program) What I want to do is : 1) Open a binary file 2) Search this file for a particular...
8
by: Gordon Knote | last post by:
Hi can anyone tell me what's the best way to search in binary content? Best if someone could post or link me to some source code (in C/C++). The search should be as fast as possible and it would...
1
by: Andrew | last post by:
Hi, im trying to create a small function which can create a binary tree from the entries in a text file and after that we can perform all the usual operations like search, insert and delete etc....
10
by: free2cric | last post by:
Hi, I have a single link list which is sorted. structure of which is like typedef struct mylist { int num; struct mylist *next;
2
by: Timmy | last post by:
The bigger problem is with the Binary Search. The program crashes when it's excuted. and Visual Studio 2005 indicates stack over flow and shows a break at that function. Sequential search is...
0
by: Vince Filby | last post by:
Hi, We are working with distributing Lucene.net. We have Master Index Server which takes responsibility of distributing the index searching to multiple Index Servers by calling the remote...
0
by: Atos | last post by:
Binary search is used to locate a value in a sorted list of values. It selects the middle element in the array of sorted values, and compares it with the target value; that is the key we are...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...

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.