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



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

[
  [: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
}
}

输出上面发布的数组,我如何获取数字,约翰的数字,例如,这样它就可以算作对布什的投票,拉斐尔对克林顿等的投票,并将它们扔到一个数组中,我可以在确定获胜者时进行最终统计?

请以最简单的方式进行管理?非常感谢!我已经十天了,甚至掌握概念也不容易 - 希望不要气馁。

从以下数组数组开始:

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]
]

越来越近!您可以使用另一种map简单地获取结果的中间值(索引[1](:

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

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

现在,它只取决于您希望如何表示此最终计数。假设您想要每个候选人的计数。有几种方法可以做到这一点,但我将使用我们上面使用的内容,一个group_by和一个map

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]]
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] }

请注意,sum 在 Rails 中可用,但在普通 Ruby 中不可用(您也可以使用 inject(

最新更新