March 2003
Intermediate to advanced
656 pages
39h 30m
English
strxfrm
strxfrm(str)Returns a string sx such that the built-in
comparison (e.g., by cmp) of strings so
transformed is equivalent to calling
locale.strcoll on the original strings.
strxfrm lets you use the decorate-sort-undecorate
(DSU) idiom for sorts that involve locale-conformant string
comparisons. However, if all you need is to sort a list of strings in
a locale-conformant way,
strcoll’s simplicity can make it
faster. The following example shows two ways of performing such a
sort; in this case, the simple variant is often faster than the DSU
one:
import locale
# simpler and often faster
def locale_sort_simple(list_of_strings):
list_of_strings.sort(locale.strcoll)
# less simple and often slower
def locale_sort_DSU(list_of_strings):
auxiliary_list = [(locale.strxfrm(s),s) for s in
list_of_strings]
auxiliary_list.sort( )
list_of_strings[:] = [s for junk, s in auxiliary_list]Read now
Unlock full access