python bisect questions

ankitks.mital at gmail.com ankitks.mital at gmail.com
Thu Apr 3 18:21:19 EDT 2008


On Apr 3, 4:24 pm, "Terry Reedy" <tjre... at udel.edu> wrote:
> <ankitks.mi... at gmail.com> wrote in message
>
> news:6bb6927b-f553-40db-a142-2ce86b9e819f at q27g2000prf.googlegroups.com...
> |I am week on functional programming, and having hard time
> | understanding this:
> |
> | class myPriorityQueue:
> |      def __init__(self, f=lamda x:x):
> |              self.A = []
> |              self.f = f
> |
> |      def append(self, item)
> |              bisect.insort(self.A, (self.f(item), item))
> |    ............
> |
> | now I know we are inserting items(user defined type objects) in list A
> | base on sorting order provided by function A.
> | but what I don't understand is bisect command
> | what does bisect.insort(self.A, (self.f(item), item)) doing

here is doc
insort_right(a, x[, lo[, hi]])

    Insert item x in list a, and keep it sorted assuming a is sorted.

    If x is already in a, insert it to the right of the rightmost x.

    Optional args lo (default 0) and hi (default len(a)) bound the
    slice of a to be searched.

but I am still confuse. self.A is my list a. and item is x that
I am trying to insert.
So it needs to be of type item not (self.f(item), item)
It doesn't say anything pass sorting function self.f(item).
Also I am new to Python
Please help?


>
> The snippet is missing 'import bisect'.  The module is documented in the
> Lib Ref.  Or, in the interpreter, help(bisect.insort) redirects you to
> help(bisect.insort_right), which will answer your question.
>
> | isn't it is returning truple of (self.f(item), item)).
>
> no, see doc
>
> | why it is not
> | biset.insort(self.A, item)
> | A.sort(f)
>
> Efficiency, given that self.A is already sorted.
>
> tjr




More information about the Python-list mailing list