Skip to main content
 首页 » 编程设计

python之包含 utf-8 字符的列表的排序

2025年05月04日11JeffreyZhao

问题的开头在这里Custom sort python

我想按这个字母表做特殊排序

alphabet = u'aáàAâÂbBcCçÇdDeéEfFgGğĞhHiİîÎíīıIjJkKlLmMnNóoOöÖpPqQrRsSşŞtTuUûúÛüÜvVwWxXyYzZ 

[aáàAâÂ] 这组字符应该具有相同的优先级。在较早的线程中,@happydave 建议使用 (alphabet.index(c)/2),

which should map each pair of adjacent characters in your list to the same priority.

但在我的情况下,我没有一对字符?例如:aáàAâÂ, eéE, uUûúÛ, óoO, iİîÎíī。

在我看来简单的方法是添加一个项目,只包含成对的字符到每个列表,但我不知道如何实现它。

 [['word1', <Element tag at b719a4cc>], ['word2', <Element tag at b719a6cc>]] 

添加了一项的列表列表

[['word1_', 'word1', <Element tag at b719a4cc>], ['word2_', 'word2', <Element tag at b719a6cc>]] 

请您参考如下方法:

s='aáàAâÂbBcCçÇdDeéEfFgGğĞhHiİîÎíīıIjJkKlLmMnNóoOöÖpPqQrRsSşŞtTuUûúÛüÜvVwWxXyYzZ' 
s2='aaaaaabbccccddeeeffgggghhiiiiiiiijjkkllmmnnoooooppqqrrssssttuuuuuuuvvwwxxyyzz' 
trans = str.maketrans(s, s2) 
 
def unikey(seq): 
    return seq[0].translate(trans) 

将此函数用作 sorted 的 key 参数