python bisect questions

ankitks.mital at gmail.com ankitks.mital at gmail.com
Fri Apr 4 17:08:57 EDT 2008


>
> b) Define a function to extract a "key" from your items such that items  
> compare the same as their keys. For example, key(x) -> x.lower() may be  
> used to compare text case-insensitively.
> Then, use a tuple (key, value) instead of the bare value. When extracting  
> items from the queue, remember to unpack both parts. This is known as the  
> decorate-sort-undecorate pattern; google for it.
> This is the approach used on your code snippet.
>
> <code>
> def keyfunc(x):
>      return x.lower()
>
> x1 = "bcd"
> x2 = "abC"
> x3 = "Z"
> x4 = "AbC"
> queue = []
> insort(queue, (keyfunc(x1),x1))
> print queue
> insort(queue, (keyfunc(x2),x2))
> print queue
> insort(queue, (keyfunc(x3),x3))
> print queue
> insort(queue, (keyfunc(x4),x4))
> print queue
> print [value for (key,value) in queue]
> </code>

I liked decorate-sort-undecorate pattern idea.
But is there anyway I can provide information for tie breaking.
so for case, where keyfunc(x) returns same values,
I need to provide additional tie-breaking rules, is that possible to
do?



More information about the Python-list mailing list