On 12/24/2012 06:19 PM, Pander Musubi wrote: > <snip> > to prevent > > Traceback (most recent call last): > File "./sort.py", line 23, in <module> > things_to_sort.sort(key=string2sortlist) > File "./sort.py", line 15, in string2sortlist > return [hashindex[s] for s in string] > KeyError: '\xc3' > > Thanks very much for this efficient code.
Perhaps you missed Ian Kelly's correction of Thomas Bach's approach: d = { k: v for v, k in enumerate(cs) } def collate(x): return list(map(d.get, x)) sorted(data, key=collate) I'd use Ian Kelly's approach. It's not only more compact, it shouldn't give an exception for a character not in the table. At least, not for Python 2.x. I'm not sure about Python 3, since it can give an exception comparing None to int. -- DaveA -- http://mail.python.org/mailman/listinfo/python-list