使用自行创建的字母表在数组中自定义排序键

发布于 2024-11-15 17:19:46 字数 915 浏览 2 评论 0 原文

在 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'
);

In python, I have the following which works perfectly well:

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

Output:

[('wr', ['A1']), ('wr-mw', ['A2']), ('wr-qA', ['A3']), ('wr,w', ['A4']), ('wr.w', ['A5']), ('wrs', ['A6']), ('wrS', ['A7']), ('wrD', ['A8']), ('Dd', ['A9']), ('=k', ['A10'])]

How can I do the same in PHP with:

$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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

少年亿悲伤 2024-11-22 17:19:46

我不完全理解你的问题,但如果你需要在 PHP 中进行自定义排序,你需要使用 usortuasort。可能是第二个,因为我看到你的数组中有自定义键。

如果您足够幸运并且可以使用 PHP 5.3,那么您可以将回调提供为闭包< /a>.

这相当于 PHP 中的用户排序。 PHP 中 indexOf 的等价物是 strpos

警告:比较 strpos 的返回值时要小心,因为如果没有找到任何匹配项,它可能会返回 false。在 PHP 中,false 等于 (==) 到 0


关于 PHP 中的列表结构。也许你需要这样的东西。

$list = array(array('wr', array('A1')), array('wr-qA',array('A3')), ...);

但不确定。

I don't fully understand your question, but if you need to do custom sorting in PHP you need to use usort or uasort. 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 be strpos.

Warning: Take care when comparing the return values of strpos because it may return false if it doesn't find any match. And in PHP false is equal (==) to 0.


About your list structure in PHP. Maybe you need something like this.

$list = array(array('wr', array('A1')), array('wr-qA',array('A3')), ...);

Not sure though.

峩卟喜欢 2024-11-22 17:19:46

对于 lambda 排序,您可以使用 usort(...) (和 strcmp() 进行比较)

usort($list, "strcmp"); // PHP function name as a string

从 PHP 5.3 开始,您还可以使用匿名函数作为回调参数

usort($list, function($a, $b) { return strcmp($a, $b) }); 
// Note - callback function must return integer comparison between 2 elements

。但是,如果按值排序,您可以简单地使用 sort() 或者如果您排序关键,使用 ksort() (请注意,它们就地排序并仅返回布尔标志)

For the lambda sorting, you can use usort(...) (and strcmp() for comparison)

usort($list, "strcmp"); // PHP function name as a string

Since PHP 5.3 you can also use an anonymous function as a callback parameter

usort($list, function($a, $b) { return strcmp($a, $b) }); 
// Note - callback function must return integer comparison between 2 elements

However, if are sorting by value, you can simply use sort() or if you sort by key, use ksort() (Note that they sort in place and return only a boolean flag)

無心 2024-11-22 17:19:46

这就是我想出来的。它使用 uksort(),它需要用户-定义的排序函数并根据元素的键对元素进行排序(这似乎是您所需要的)。

该代码可能需要一些调整,但我尝试过并且它有效。调用 uksort() 后,$list 变量将包含排序后的数组。

在此代码示例中,我使用 匿名函数 作为排序函数,即从 PHP 5.3 开始可用,在此之前,您可以使用一个简单的函数(例如,您可以查看我之前链接的 uksort() 参考资料)。

$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');
$alphabet = " -,.AjawbpfmnrhHxXsSqkgtTdD=/()[]<>{}'*#I1234567890&@";

uksort($list, function ($a, $b) use ($alphabet) {
    $shorter=min($a, $b);
    $len=strlen($shorter);
    for ($i=0, $len=strlen($shorter); $i < $len; $i++) {
        $aval=strpos($alphabet, $a[$i]);
        $bval=strpos($alphabet, $b[$i]);
        if ($aval!=$bval) {
            return $aval > $bval ? 1 : -1;
        }
    }
    return $shorter==$b ? 1 : -1;
});

编辑:我很快编写了一个没有匿名函数的版本:

$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');

function alphabet_sorter($a, $b) {
    $alphabet = " -,.AjawbpfmnrhHxXsSqkgtTdD=/()[]<>{}'*#I1234567890&@";

    $shorter=min($a, $b);
    $len=strlen($shorter);
    for ($i=0, $len=strlen($shorter); $i < $len; $i++) {
        $aval=strpos($alphabet, $a[$i]);
        $bval=strpos($alphabet, $b[$i]);
        if ($aval!=$bval) {
            return $aval > $bval ? 1 : -1;
        }
    }
    return $shorter==$b ? 1 : -1;
}

uksort($list, 'alphabet_sorter');

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).

$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');
$alphabet = " -,.AjawbpfmnrhHxXsSqkgtTdD=/()[]<>{}'*#I1234567890&@";

uksort($list, function ($a, $b) use ($alphabet) {
    $shorter=min($a, $b);
    $len=strlen($shorter);
    for ($i=0, $len=strlen($shorter); $i < $len; $i++) {
        $aval=strpos($alphabet, $a[$i]);
        $bval=strpos($alphabet, $b[$i]);
        if ($aval!=$bval) {
            return $aval > $bval ? 1 : -1;
        }
    }
    return $shorter==$b ? 1 : -1;
});

EDIT: I quickly wrote a version without an anonymous function:

$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');

function alphabet_sorter($a, $b) {
    $alphabet = " -,.AjawbpfmnrhHxXsSqkgtTdD=/()[]<>{}'*#I1234567890&@";

    $shorter=min($a, $b);
    $len=strlen($shorter);
    for ($i=0, $len=strlen($shorter); $i < $len; $i++) {
        $aval=strpos($alphabet, $a[$i]);
        $bval=strpos($alphabet, $b[$i]);
        if ($aval!=$bval) {
            return $aval > $bval ? 1 : -1;
        }
    }
    return $shorter==$b ? 1 : -1;
}

uksort($list, 'alphabet_sorter');
Spring初心 2024-11-22 17:19:46

使用与我对完全相同的uksort()函数//stackoverflow.com/q/6350510/2943403">您之前的问题,您只需扩展翻译字符串即可对自定义字母表中的非空格字母使用大写和小写字母。我从两个翻译字符串中撤回了空格,因为它们都位于第一个位置,并且翻译毫无意义。

代码:(演示

$trans = [
    "-,.AjawbpfmnrhHxXsSqkgtTdD=/()[]<>{}'*#I1234567890&@",
    "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
];

$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'
];

uksort(
    $list,
    function ($a, $b) use ($trans) {
        return strtr($a, ...$trans) <=> strtr($b, ...$trans);
    }
);

var_export($list);

输出:

array (
  'wr' => 'A1',
  'wr-mw' => 'A2',
  'wr-qA' => 'A3',
  'wr,w' => 'A4',
  'wr.w' => 'A5',
  'wrs' => 'A6',
  'wrS' => 'A7',
  'wrD' => 'A8',
  'Dd' => 'A9',
  '=k' => 'A10',
)

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)

$trans = [
    "-,.AjawbpfmnrhHxXsSqkgtTdD=/()[]<>{}'*#I1234567890&@",
    "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
];

$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'
];

uksort(
    $list,
    function ($a, $b) use ($trans) {
        return strtr($a, ...$trans) <=> strtr($b, ...$trans);
    }
);

var_export($list);

Output:

array (
  'wr' => 'A1',
  'wr-mw' => 'A2',
  'wr-qA' => 'A3',
  'wr,w' => 'A4',
  'wr.w' => 'A5',
  'wrs' => 'A6',
  'wrS' => 'A7',
  'wrD' => 'A8',
  'Dd' => 'A9',
  '=k' => 'A10',
)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文