best way to determine sequence ordering?

I V wrongbad at gmail.com
Fri Apr 28 18:08:19 EDT 2006


On Fri, 28 Apr 2006 14:27:00 -0700, nikie wrote:
> Steven Bethard wrote:
> 
>>  >>> L = ['C', 'A', 'D', 'B']
>>  >>> positions = dict((item, i) for i, item in enumerate(L))

Do you need the generator expression here? dict(enumerate(L)) should be
equivalent, no?

> Isn't this bound to be less efficient? I mean, inserting the items into
> a dict is probably O(n log n), which is definitely worse than O(n) for
> searching the list twice. And, of course, it would yield different
> results if 'A' or 'D' are in the list more than once.

Although presumably the dict method might be quicker if you were comparing
the positions a large number of times.

Incidentally, does python have a built-in to do a binary search on a
sorted list? Obviously it's not too tricky to write one, but it would be
nice if there was one implemented in C.



More information about the Python-list mailing list