使用自行创建的字母表在数组中自定义排序键
在 python 中,我有以下效果非常好的输出:
list = [('wr', ['A1']), ('wr-qA', ['A3']), ('wr,w', ['A4']), ('wr-mw', ['A2']), ('wrs', ['A6']), ('wrD', ['A8']), ('wrS', ['A7']), ('wr.w', ['A5']), ('=k', ['A10']), ('Dd', ['A9'])]
alphabet = " -,.AjawbpfmnrhHxXsSqkgtTdD=/()[]<>{}'*#I1234567890&@"
Sorted_list = sorted(list, key=lambda (v, k): [alphabet.index(c) for c in v])
print Sorted_list
输出:
[('wr', ['A1']), ('wr-mw', ['A2']), ('wr-qA', ['A3']), ('wr,w', ['A4']), ('wr.w', ['A5']), ('wrs', ['A6']), ('wrS', ['A7']), ('wrD', ['A8']), ('Dd', ['A9']), ('=k', ['A10'])]
如何在 PHP 中执行相同的操作:
$list = array(
'wr' => 'A1',
'wr-qA' => 'A3',
'wr,w' => 'A4',
'wr-mw' => 'A2',
'wrs' => 'A6',
'wrD' => 'A8',
'wrS' => 'A7',
'wr.w' => 'A5',
'=k' => 'A10',
'Dd' => 'A9'
);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我不完全理解你的问题,但如果你需要在 PHP 中进行自定义排序,你需要使用
usort
或uasort
。可能是第二个,因为我看到你的数组中有自定义键。如果您足够幸运并且可以使用 PHP 5.3,那么您可以将回调提供为闭包< /a>.
这相当于 PHP 中的用户排序。 PHP 中
indexOf
的等价物是strpos
。警告:比较
strpos
的返回值时要小心,因为如果没有找到任何匹配项,它可能会返回false
。在 PHP 中,false
等于 (==
) 到0
。关于 PHP 中的列表结构。也许你需要这样的东西。
但不确定。
I don't fully understand your question, but if you need to do custom sorting in PHP you need to use
usort
oruasort
. Probably the second one, as I see you have custom key in your array.If you're lucky enough and can use PHP 5.3 than you may supply the callback as a closure.
This will be the equivalent in PHP of user sorting. The equivalent of
indexOf
in PHP would bestrpos
.Warning: Take care when comparing the return values of
strpos
because it may returnfalse
if it doesn't find any match. And in PHPfalse
is equal (==
) to0
.About your list structure in PHP. Maybe you need something like this.
Not sure though.
对于 lambda 排序,您可以使用
usort(...)
(和
strcmp()
进行比较)从 PHP 5.3 开始,您还可以使用匿名函数作为回调参数
。但是,如果按值排序,您可以简单地使用
sort()
或者如果您排序关键,使用ksort()
(请注意,它们就地排序并仅返回布尔标志)For the lambda sorting, you can use
usort(...)
(andstrcmp()
for comparison)Since PHP 5.3 you can also use an anonymous function as a callback parameter
However, if are sorting by value, you can simply use
sort()
or if you sort by key, useksort()
(Note that they sort in place and return only a boolean flag)这就是我想出来的。它使用
uksort()
,它需要用户-定义的排序函数并根据元素的键对元素进行排序(这似乎是您所需要的)。该代码可能需要一些调整,但我尝试过并且它有效。调用
uksort()
后,$list
变量将包含排序后的数组。在此代码示例中,我使用 匿名函数 作为排序函数,即从 PHP 5.3 开始可用,在此之前,您可以使用一个简单的函数(例如,您可以查看我之前链接的
uksort()
参考资料)。编辑:我很快编写了一个没有匿名函数的版本:
This is what I came up with. It uses
uksort()
, which takes a user-defined sorting function and sorts the elements based on their keys (which is what you need as it seems).The code might need a little tweaking, but I tried it and it works. After calling
uksort()
, the$list
variable will contain the sorted array.In this code example, I used an Anonymous function as a sorting function, which is available from PHP 5.3, before that, you can use a simple function (for examples, you can check the
uksort()
reference I linked earlier).EDIT: I quickly wrote a version without an anonymous function:
使用与我对完全相同的
uksort()
函数//stackoverflow.com/q/6350510/2943403">您之前的问题,您只需扩展翻译字符串即可对自定义字母表中的非空格字母使用大写和小写字母。我从两个翻译字符串中撤回了空格,因为它们都位于第一个位置,并且翻译毫无意义。代码:(演示)
输出:
Using the exact same
uksort()
function as my answer to your earlier question, you only need to extend the translation string to use upper and lower case letter for non-space letters in your custom alphabet. I withdrew the space from both translation strings because they would both be in the first position and the translation is pointless.Code: (Demo)
Output: