> > 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? -- http://mail.python.org/mailman/listinfo/python-list