perl – 学习元素的顺序
发布时间:2020-12-16 06:09:57 所属栏目:大数据 来源:网络整理
导读:我想找到一种有效的方法(最好是在Perl中)通过比较它们在组的多个子集中的顺序来学习单词族的固定顺序. (它们是工作参数.大约有30种不同的工作参数.不同的工作需要不同的参数组合,并且每个工作中只有一些参数) 例如,给定: firstsecondthirdsixthseventhtenth
我想找到一种有效的方法(最好是在Perl中)通过比较它们在组的多个子集中的顺序来学习单词族的固定顺序.
(它们是工作参数.大约有30种不同的工作参数.不同的工作需要不同的参数组合,并且每个工作中只有一些参数) 例如,给定: first second third sixth seventh tenth first third fourth fifth sixth third fifth seventh eighth ninth tenth 它应该能够记住它看到的相对顺序关系,以确定订单是: first second third fourth fifth sixth seventh eighth ninth tenth 我已生成如下列表: first.second.third.sixth.seventh.tenth first.third.fourth.fifth.sixth third.fifth.seventh.eighth.ninth.tenth 然后按字母顺序排序,并在视觉上对它们进行比较,但是我有30个参数的数百种不同的组合,所以将它们全部排序并手动将它们放在一起将是一项很大的工作. 我认为@ daniel-tran已经回答了https://stackoverflow.com/a/48041943/224625中的“how”并使用了一些hackery: $order->{$prev}->{$this} = 1; $order->{$this}->{$prev} = 0; 我设法为每对连续参数填充哈希值为1或0的哈希值,以说明哪个是第一个,如: $VAR1 = { 'first' => { 'second' => 1,'third' => 1,},'second' => { 'first' => 0,'third' => { 'first' => 0,'second' => 0,'fourth' => 1,'fifth' => 1,'sixth' => 1,'fourth' => { 'third' => 0,'fifth' => 1,... 但当我被要求排序一对从未被视为直接邻居的一对时,我试图弄清楚我的排序功能该做什么,因此没有定义关系. 有一个简单的解决方案吗? 谢谢, 约翰 解决方法
使用图表和拓扑排序链接到
includes another answer的问题.
Graph 模块非常易于使用:
use warnings; use strict; use Graph; my $graph = Graph->new(directed => 1); my $prev; while (<DATA>) { chomp; $graph->add_edge($prev,$_) if length && length $prev; $prev = $_; } print $_,"n" for $graph->topological_sort; __DATA__ first second third sixth seventh tenth first third fourth fifth sixth third fifth seventh eighth ninth tenth 输出: first second third fourth fifth sixth seventh eighth ninth tenth (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |