[Tutor] Binary search question

Emile van Sebille emile at fenx.com
Fri Apr 23 23:33:31 CEST 2010


On 4/23/2010 2:21 PM Alan Gauld said...
>
> "Emile van Sebille" <emile at fenx.com> wrote
>> It's expensive enough that for a list this size I'd convert it to a
>> dict and use in on that. eg,
>>
>> a = range(100000)
>> d = dict(zip(a,a))
>>
>
> Surely that would depend on how often you do the search? If its a one
> off occurence I'd expect the overhead of zipping and converting to a
> dict would outweight the savings?

Oh sure, but in practical terms, if it's a one-off situation who cares 
which you us?  For a one-off I'd just use in and not be concerned.

>
> If the search was inside a loop however then I'd definitely agree.
> Although I'd opt for a set rather than a dict...

Old habits...

Emile



More information about the Tutor mailing list