Python Glossary Русский

all Python 3localefunction

locale.strxfrm()

string collation

Transforms a string into a key that compares with plain < the way the originals compare under strcoll(); handy as a key for sorted().

Syntax

sorted(words, key=locale.strxfrm)

sorted(words, key=locale.strxfrm) beats cmp_to_key(strcoll) because each string is transformed once. The key is an opaque string for the current LC_COLLATE: storing it in a file or comparing after a locale change is meaningless. In the 'C' locale the key equals the string itself, so without setlocale() Russian sorting stays by code point ('ё' after 'я').

Examples

import locale
print(sorted(['b', 'A', 'a'], key=locale.strxfrm))  # → ['A', 'a', 'b'] — локаль C
print(locale.strxfrm('abc'))  # → abc — в C ключ равен строке

See also

Open in the full glossarydocs.python.orgReport an inaccuracy