On Apr 3, 5:43 pm, John Machin <[EMAIL PROTECTED]> wrote: > On Apr 4, 9:21 am, [EMAIL PROTECTED] wrote: > > > > > > > On Apr 3, 4:24 pm, "Terry Reedy" <[EMAIL PROTECTED]> wrote: > > > > <[EMAIL PROTECTED]> wrote in message > > > >news:[EMAIL PROTECTED] > > > |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). > > That's correct. You are passing a tuple of (sort_key, actual_data). > > Example use case: caseless sortorder but you want to retrieve the > original data. f is lambda x: x.upper() or similar. Your data is > 'foo', 'Bar', 'zOt'. Calls to your_queue.append will result in the > following 2nd args for bisect.insort: > ('FOO', 'foo') > ('BAR', 'Bar') > ('ZOT', 'zOt')
Thanks John and Terry, Wow! great way to keep original values with transformation. Thanks for explaining. > > Consider executing the code with a couple of print statements in it so > that you can see what is happening.- Hide quoted text - > > - Show quoted text - -- http://mail.python.org/mailman/listinfo/python-list