根据Python中的自定义字母对字符串值进行排序 [英] Sorting string values according to a custom alphabet in Python

查看:486
本文介绍了根据Python中的自定义字母对字符串值进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种有效的方法来根据自定义字母对字符串列表进行排序.

I am looking for an efficient way to sort a list of strings according a custom alphabet.

例如,我有一个字符串字母"bafmxpzv"和一个仅由该字母中包含的字符组成的字符串列表.

For example, I have a string alphabet which is "bafmxpzv" and a list of strings composed from only the characters contained in that alphabet.

我想要一种类似于其他常见排序方式对列表进行排序的方法,但是要使用此自定义字母.我该怎么办?

I would like a way to sort that list similarly to other common sorts, but using this custom alphabet. How can I do that?

推荐答案

让我们创建一个字母和一个单词列表:

Let's create an alphabet and a list of words:

In [32]: alphabet = "bafmxpzv"

In [33]: a = ['af', 'ax', 'am', 'ab', 'zvpmf']

现在,让我们根据字母在alphabet中出现的位置对它们进行排序:

Now let's sort them according to where the letters appear in alphabet:

In [34]: sorted(a, key=lambda word: [alphabet.index(c) for c in word])
Out[34]: ['ab', 'af', 'am', 'ax', 'zvpmf']

以上内容以正确的顺序排序.

The above sorts in the correct order.

sorted支持广泛的自定义排序. sorted函数具有三个可选参数:cmpkeyreverse:

sorted enables a wide range of custom sorting. The sorted function has three optional arguments: cmp, key, and reverse:

  • cmp适用于复杂的排序任务.如果指定,cmp应该是一个带有两个参数的函数.它应返回负数,零数或正数,具体取决于第一个自变量是否小于,等于或大于第二个自变量.在这种情况下,cmp太过分了.

  • cmp is good for complex sorting tasks. If specified, cmp should be a functionIt that takes two arguments. It should return a negative, zero or positive number depending on whether the first argument is considered smaller than, equal to, or larger than the second argument. For this case, cmp is overkill.

key(如果经过精化)应该是一个带有一个参数并返回python本身知道如何排序的函数.在这种情况下,key将返回字母中每个单词字符的索引列表.

key, if spedified, should be a function that takes one argument and returns something that python knows natively how to sort. In this case, key returns a list of the indices of each of the word's characters in the alphabet.

在这种情况下,key返回alphabet中字母的索引.

In this case, key returns the index of a letter in alphabet.

reverse,如果为true,则反转排序顺序.

reverse, if true, reverses the sort-order.

在评论中,提到了这种替代形式:

From the comments, this alternative form was mentioned:

In [35]: sorted(a, key=lambda word: [alphabet.index(c) for c in word[0]])
Out[35]: ['af', 'ax', 'am', 'ab', 'zvpmf']

请注意,这没有按照正确的顺序排序.这是因为此处的key函数仅考虑每个单词的第一个字母.这可以通过测试key来证明:

Note that this does not sort in the correct order. That is because the key function here only considers the first letter of each word. This can be demonstrated by testing key:

In [2]: key=lambda word: [alphabet.index(c) for c in word[0]]

In [3]: key('af')
Out[3]: [1]

In [4]: key('ax')
Out[4]: [1]

观察到key对于两个不同的字符串afax返回相同的值.返回的值仅反映每个单词的第一个字符.因此,sorted无法确定afax之前属于.

Observe that key returns the same value for two different strings, af and ax. The value returned reflects only the first character of each word. Because of this, sorted has no way of determining that af belongs before ax.

这篇关于根据Python中的自定义字母对字符串值进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆