Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby 查找散列中具有最高值的N个键,保持顺序_Ruby_Sorting_Hash - Fatal编程技术网

Ruby 查找散列中具有最高值的N个键,保持顺序

Ruby 查找散列中具有最高值的N个键,保持顺序,ruby,sorting,hash,Ruby,Sorting,Hash,在Ruby脚本中 我有一个散列,以句子为键,相关性分数为值 我想检索一个数组,其中包含N个最相关的句子(最高分数) 我想保留这些句子的提取顺序 鉴于: hash = { 'This is the first sentence.' => 5, 'This is the second sentence.' => 1, 'This is the last sentence.' => 6 } 然后: 应返回: ['This is the first sentence.

在Ruby脚本中

  • 我有一个散列,以句子为键,相关性分数为值
  • 我想检索一个数组,其中包含N个最相关的句子(最高分数)
  • 我想保留这些句子的提取顺序
鉴于:

hash = {
  'This is the first sentence.' => 5,
  'This is the second sentence.' => 1,
  'This is the last sentence.' => 6
}
然后:

应返回:

['This is the first sentence.', 'This is the last sentence.']

我能想到的所有方法都涉及对散列进行重新排序,从而失去句子的顺序。解决这个问题的最佳方法是什么?

试试下面的怪物:

hash.map(&:reverse).each_with_index
                   .sort_by(&:first).reverse
                   .take(2)
                   .sort_by(&:last)
                   .map { |(_,s),_| s }
另一个功能:

hash.to_a.values_at(*hash.values.each_with_index
                         .sort.reverse
                         .map(&:last)
                         .sort.take(2))
         .map(&:first)
但是请注意,作为一种无序的数据结构,哈希表并不真正适合这种用例(尽管Ruby 1.9中记住了顺序)。您应该改用数组(排序代码保持不变):


尝试以下怪物:

hash.map(&:reverse).each_with_index
                   .sort_by(&:first).reverse
                   .take(2)
                   .sort_by(&:last)
                   .map { |(_,s),_| s }
另一个功能:

hash.to_a.values_at(*hash.values.each_with_index
                         .sort.reverse
                         .map(&:last)
                         .sort.take(2))
         .map(&:first)
但是请注意,作为一种无序的数据结构,哈希表并不真正适合这种用例(尽管Ruby 1.9中记住了顺序)。您应该改用数组(排序代码保持不变):

数组a现在包含得分最高的句子的成对值。您可以选择其中的前N个

hash = {"foo" => 7, "bar" => 2, "blah" => 3 }
a = hash.sort_by { |sentence, score| score }.reverse
=> [["foo", 7], ["blah", 3], ["bar", 2]]
数组a现在包含得分最高的句子的成对值。您可以选择其中的前N个

hash = {"foo" => 7, "bar" => 2, "blah" => 3 }
a = hash.sort_by { |sentence, score| score }.reverse
=> [["foo", 7], ["blah", 3], ["bar", 2]]
def提取哈希,n
min=hash.values.sort[-n]
a=[]
i=0
hash.each{k,v |(a.push(k)和i+=1),如果i=min}
A.
结束
def提取哈希,n
min=hash.values.sort[-n]
a=[]
i=0
hash.each{k,v |(a.push(k)和i+=1),如果i=min}
A.
结束
从Ruby 2.2.0开始,采用一个可选的整数参数,使其返回一个数组,而不仅仅是一个元素。因此,我们可以做到:

hash = {
  'This is the first sentence.' => 6,
  'This is the second sentence.' => 1,
  'This is the last sentence.' => 5
 }

p hash.max_by(2, &:last).map(&:first).sort_by { |k| hash.keys.index k }
# => ["This is the first sentence.", "This is the last sentence."]
最后对
sort_by
的调用保证了句子的顺序符合您的要求。

从Ruby 2.2.0开始,使用可选的整数参数,使其返回数组而不是单个元素。因此,我们可以做到:

hash = {
  'This is the first sentence.' => 6,
  'This is the second sentence.' => 1,
  'This is the last sentence.' => 5
 }

p hash.max_by(2, &:last).map(&:first).sort_by { |k| hash.keys.index k }
# => ["This is the first sentence.", "This is the last sentence."]


最后对
sort\u by
的调用保证了句子按照您要求的顺序排列正确。

您现在知道散列没有顺序了,对吗?你想让这个Ruby仅为1.9吗?如果有带连号的句子,并且你不能精确地选择N个最大值,你想发生什么?据我所知,哈希不是“制造”成有序的,尽管插入顺序将保持在Ruby>1.9中。我早就将这个特定的应用程序提交给Ruby>1.9,所以打破BC并不会让我太烦恼。但如果有一个哲学上更好的方法来实现这一点,请启发我@路易丝:我试着在我的回答中这样做:)@sawa,在提取句子之前,我会在代码中的其他地方处理关系——假设不会有关系。你现在知道散列没有顺序了,对吗?你想让这个Ruby仅为1.9吗?如果有带连号的句子,并且你不能精确地选择N个最大值,你想发生什么?据我所知,哈希不是“制造”成有序的,尽管插入顺序将保持在Ruby>1.9中。我早就将这个特定的应用程序提交给Ruby>1.9,所以打破BC并不会让我太烦恼。但如果有一个哲学上更好的方法来实现这一点,请启发我@路易丝:我试着在我的回答中这样做:)@sawa,在提取句子之前,我会在代码的其他地方处理关系——只要假设不会有关系。其中一个要求是保持原始顺序。我的坏习惯。当他谈到重新排列散列时,我误解了他的要求。我以为他在寻找不会扰乱散列顺序的东西(即不要提出涉及更改散列顺序的解决方案)。要求之一是保留原始顺序。我的错。当他谈到重新排列散列时,我误解了他的要求。我以为他在寻找一些不会扰乱散列顺序的东西(即不要提出涉及更改散列顺序的解决方案)。太好了!你能解释一下
|(124;,s),124; s
做了什么,或者链接到包含更多信息的页面吗?我以前从来没有见过这样的事情。@louism:这才是最难看的部分。在排序的过程中,首先按降序排序,然后按升序索引排序,我选择将项目表示为
[[rank,station],index]
格式的数组。排序之后,我们只想提取句子,因此我们使用数组解构/模式匹配将每个项目分配给块内的
(\us),\uz
,这将丢弃秩和索引,并将句子分配给变量
s
。谢谢。你的第二种方法比第一种方法稍微好一点,但它只适用于散列,所以我想我会选择第一种。@louism:如果你关心速度,你肯定会比我的两种方法做得更好。它们实际上并不十分有效,但除非证明有必要,否则我通常不会进行预优化,并尽可能使代码可读,而不是尽可能快。第二段代码也适用于数组,它只需要最小的自适应(不需要
。到a
。映射(&:last)
,而不是
。值
)。@louism:这还远远不是最快的。此外,该测试集应该几乎不现实。我只能重复一次,如果实际分析结果表明此特定代码是应用程序中的瓶颈,那么我只需要担心性能。如果情况并非如此,请选择可读性而不是速度,并优化代码中经常执行的部分。太棒了!你能解释一下
|(124;,s),124; s
做了什么,或者链接到包含更多信息的页面吗?我以前从来没有见过这样的事情。@louism:这才是最难看的部分。在排序的过程中,首先按降序排序,然后按升序索引排序,我选择将项目表示为表单的数组
hash = {
  'This is the first sentence.' => 6,
  'This is the second sentence.' => 1,
  'This is the last sentence.' => 5
 }

p hash.max_by(2, &:last).map(&:first).sort_by { |k| hash.keys.index k }
# => ["This is the first sentence.", "This is the last sentence."]