如何比较嵌入在数组中的两个键值对?

时间:2015-07-11 11:15:29

标签: arrays ruby

如果我有两名候选人和五名选民,投票倾向的结果是:

[
  [:John, :Clinton, -27],
  [:John, :Bush, -8],
  [:Raphael, :Clinton, -12],
  [:Raphael, :Bush, -40],
  [:Damon, :Clinton, 71],
  [:Damon, :Bush, 4],
  [:Elysee, :Clinton, 13], 
  [:Elysee, :Bush, -36],
  [:Griffin, :Clinton, -1],
  [:Griffin, :Bush, 11]
]

我如何看待每位选民,并将最大的趋势数作为该候选人的投票数,并将其存储在每个候选人的最终投票数的变量中?

您好!非常感谢回复。我不仅是编程的新手,我也是Stackoverflow的新手,所以请原谅我,如果我没有正确编辑。

道歉。

阅读你们所说的内容,我甚至不知道如何处理这些内容,因此我会提出我想出的代码并尝试更好地解释我想要实现的目标。忍受我。

如果我有:

def stump_speech
  voter_list = {
    John: "Progressive",
    Raphael: "Conservative",
    Damon: "Libertarian",
    Elysee: "Independent",
    Griffin: "Massachussetts Democrat"
  }
end

并将其与:

进行比较
candidate_list = {}
  candidate_list = {
    Clinton: "Democrat",
    Bush: "Republican",
}

运行它的是:

voter_list.each { |voter_name, politics|
candidate_list.each { |candidate_name, party|
  if
    politics == "Progressive" && party == "Republican"
    voting_prob = rand(0..100) - 25
    decision
  elsif
    politics == "Progressive" && party == "Democrat"
    voting_prob = rand(0..100) - 75
    decision
  elsif
    politics == "Conservative" && party == "Republican"
    voting_prob = rand(0..100) - 75
    decision
  elsif
    politics == "Conservative" && party == "Democrat"
    voting_prob = rand(0..100) - 25
    decision
  elsif
    politics == "Independent" && party == "Republican"
    voting_prob = rand(0..100) - 50
    decision
  elsif
    politics == "Independent" && party == "Democrat"
    voting_prob = rand(0..100) - 50
    decision
  elsif
    politics == "Libertarian" && party == "Republican"
    voting_prob = rand(0..100) - 90
    decision
  elsif
    politics == "Libertarian" && party == "Democrat"
    voting_prob = rand(0..100) - 10
    decision
  elsif
    politics == "Massachussetts Democrat" && party == "Republican"
    voting_prob = rand(0..100) - 10
    decision
  elsif
    politics == "Massachussetts Democrat" && party == "Democrat"
    voting_prob = rand(0..100) - 90
    decision
  else
  end
}
}

它输出上面发布的数组,我如何获取数字,约翰的例子,这样它将被视为对布什的投票,而拉斐尔对克林顿的投票等等,把它们扔进一个阵列,我可以用它来决定胜利者的最终记录?

请以最简单的方式管理?非常感谢!我进入这十天,甚至把握概念并不容易 - 希望不要气馁。

2 个答案:

答案 0 :(得分:1)

从以下数组数组开始:

tendencies = [
  [:John, :Clinton, -27],
  [:John, :Bush, -8],
  [:Raphael, :Clinton, -12],
  [:Raphael, :Bush, -40],
  [:Damon, :Clinton, 71],
  [:Damon, :Bush, 4],
  [:Elysee, :Clinton, 13], 
  [:Elysee, :Bush, -36],
  [:Griffin, :Clinton, -1],
  [:Griffin, :Bush, 11]
]

首先,我会使用Enumerable的group_by按选民组织,每个数组中的first条目。

tendencies.group_by {|t| t.first}

或等同地

tendencies.group_by(&:first)

这导致以下哈希:

{
  :John    =>[[:John, :Clinton, -27], [:John, :Bush, -8]],
  :Raphael =>[[:Raphael, :Clinton, -12], [:Raphael, :Bush, -40]]
  :Damon   =>[[:Damon, :Clinton, 71], [:Damon, :Bush, 4]],
  :Elysee  =>[[:Elysee, :Clinton, 13], [:Elysee, :Bush, -36]],
  :Griffin =>[[:Griffin, :Clinton, -1], [:Griffin, :Bush, 11]]
}

现在,我假设你每个选民都想要一名候选人。因此,如果我们查看上面的values中的每一个,就会有两个选项,我们想要一个max的选项。由于我们每个值都需要一个候选项,因此我们可以使用map。例如,要获得布什的选票,我们可以做到以下几点:

tendencies.group_by(&:first).values.map(&:last)

这将从上面的每一行获取最后一个(第二个)子阵列:

[
  [:John, :Bush, -8],
  [:Raphael, :Bush, -40],
  [:Damon, :Bush, 4],
  [:Elysee, :Bush, -36],
  [:Griffin, :Bush, 11]
]

但是我们不想要这个,不是吗?我们希望具有max趋势值的那个。所以我们需要使用max_by。这将根据我们指定的任何条件采用最大值,在本例中为每个子数组的last值(趋势)。

tendencies.group_by(&:first).values.map{ |v| v.max_by(&:last)}

结果:

[
 [:John, :Bush, -8],
 [:Raphael, :Clinton, -12],
 [:Damon, :Clinton, 71],
 [:Elysee, :Clinton, 13],
 [:Griffin, :Bush, 11]
]

越来越近了!您可以使用另一个[1]来获取结果的中间值(索引map):

tendencies.group_by(&:first).values.map{ |v| v.max_by(&:last)}.map{ |v| v[1]}

结果:[:Bush, :Clinton, :Clinton, :Clinton, :Bush]

现在它只取决于你想要如何表示这个最终的结果。假设你想要每个候选人的数量。有a few ways来做,但我将使用我们上面使用的内容,group_bymap

votes = [:Bush, :Clinton, :Clinton, :Clinton, :Bush]

votes.group_by{|v| v}

# => {:Bush=>[:Bush, :Bush], :Clinton=>[:Clinton, :Clinton, :Clinton]}

votes.group_by{|v| v}.map{|candidate, votes| [candidate, votes.count]}

# => [[:Bush, 2], [:Clinton, 3]]

答案 1 :(得分:0)

array = 
[[:John, :Clinton, -27],
[:John, :Bush, -8],
[:Raphael, :Clinton, -12],
[:Raphael, :Bush, -40],
[:Damon, :Clinton, 71],
[:Damon, :Bush, 4],
[:Elysee, :Clinton, 13],
[:Elysee, :Bush, -36],
[:Griffin, :Clinton, -1],
[:Griffin, :Bush, 11]]

看看约翰的事:

array.select { |rec| rec[0] == :John } # => [[:John, :Clinton, -27], [:John, :Bush, -8]]

或者获得布什总票数

vote_sum = 0
array.select { |rec| rec[1] == :Bush }.each { |rec| vote_sum += rec[-1] }

请注意,总和在Rails中可用,但不是普通的Ruby(您也可以使用注入)