perl:随机播放值排序的哈希值?
首先抱歉我的英语 - 我希望你能理解我。
有一个散列:
$hash{a} = 1;
$hash{b} = 3;
$hash{c} = 3;
$hash{d} = 2;
$hash{e} = 1;
$hash{f} = 1;
我想按值(而不是键)对它进行排序,所以我有:
for my $key ( sort { $hash{ $a } <=> $hash{ $b } } keys %hash ) { ... }
首先我得到所有值为 1 的键,然后值为 2 等......太棒了。
但如果散列没有改变,键的顺序(按值排序)总是相同的。
问题:如何打乱排序结果,以便每次运行“for”循环时,我都会得到不同顺序的键值 1、值 2 等?
At first sorry for my english - i hope you will understand me.
There is a hash:
$hash{a} = 1;
$hash{b} = 3;
$hash{c} = 3;
$hash{d} = 2;
$hash{e} = 1;
$hash{f} = 1;
I want to sort it by values (not keys) so I have:
for my $key ( sort { $hash{ $a } <=> $hash{ $b } } keys %hash ) { ... }
And at first I get all the keys with value 1, then with value 2, etc... Great.
But if hash is not changing, the order of keys (in this sort-by-value) is always the same.
Question: How can I shuffle sort-results, so every time I run 'for' loop, I get different order of keys with value 1, value 2, etc. ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
不太确定我很了解您的需求,但这可以吗:
Not quite sure I well understand your needs, but is this ok:
您可以简单地添加另一个排序级别,当常规排序方法无法区分两个值时将使用该排序级别。例如:
例如:
You can simply add another level of sorting, which will be used when the regular sorting method cannot distinguish between two values. E.g.:
For example:
要按值对键进行排序,并对具有相同值的键进行随机排序,我看到两种解决方案:
或者
需要
use sort 'stable';
来防止sort
损坏shuffle
返回的列表的随机性。上面对 Schwartzian 变换 的使用并不是优化的尝试。我见过人们在比较函数本身中使用 rand 来尝试实现上述结果,但这样做存在错误,原因有两个。
当使用诸如此类的“行为不当”比较时,结果会被记录为未定义,因此
sort
允许返回垃圾、重复元素、缺失元素等。
sort
不会返回垃圾,它不会是公平的排序。将对结果进行权衡。To sort the keys by value, with random ordering of keys with identical values, I see two solutions:
or
The
use sort 'stable';
is required to preventsort
from corrupting the randomness of the list returned byshuffle
.The above's use of the Schwartzian Transform is not an attempt at optimisation. I've seen people use
rand
in the compare function itself to try to achieve the above result, but doing so is buggy for two reasons.When using "misbehaving" comparisons such as that, the results are documented as being undefined, so
sort
is allowed to return garbage, repeated elements, missing elements, etc.Even if
sort
doesn't return garbage, it won't be a fair sort. The result will be weighed.您可以有两个用于升序和降序的函数,并相应地使用它们,例如
You can have two functions for ascending and decending order and use them accordingly like
看来您想随机化按键循环。
Perl 不按顺序或排序顺序存储,但这对您来说似乎不够随机,因此您可能需要创建一个键数组并循环遍历它。
首先,用键填充数组,然后使用随机数算法 (1..$#length_of_array) 将数组中该位置的键推送到 array_of_keys。
如果您尝试随机化按值排序哈希的键,那就有点不同了。
查看键盘
It seems like you want to randomize looping through the keys.
Perl, does not store in sequential or sorted order, but this doesn't seem to be random enough for you, so you may want to create an array of keys and loop through that instead.
First, populate an array with keys, then use a random number algorithm (1..$#length_of_array) to push the key at that position in the array, to the array_of_keys.
If you're trying to randomize the keys of the sorted-by-value hash, that's a little different.
See Codepad