Name
strxfrm
Synopsis
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]
Get Python in a Nutshell now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.