1

我正在为 Ruby 中的数据结构而苦苦挣扎。

我有:

answers = [
    {"val"=>["6"], "comment"=>["super"], "qid"=>["2"]},
    {"val"=>["3"], "comment"=>[""], "qid"=>["1"]},
    {"val"=>["7"], "comment"=>[""], "qid"=>["4"]},
    {"val"=>["5", "6"], "comment"=>["supera", "hmm"], "qid"=>["1", "2"]},
    {"val"=>["5", "9"], "comment"=>["super", "asdf"], "qid"=>["1", "5"]}
]

我需要以下 qid 数组,它们应该是唯一的,在所有哈希中:

["2","1","4","5"] # note, value 2 exists two times and value 1, 3 times 

相应的值应汇总并除以计数:

["12","13","7","9"] will be: ["6","4.3","7","9"] # 12/2 and 13/3

还应总结评论:

[["super","hmm"],["","supera","super"],[""],["asdf"]]

我想知道把它放在一个哈希中是否很酷?

到目前为止,我有:

a = Hash.new(0)
  answers.each.map { |r| r }.each do |variable|
    variable["qid"].each_with_index do |var, index|
        #a[var] << { :count => a[var][:count] += 1 }
        #a[var]["val"] += variable["val"][index]
        #a[var]["comment"] = a[var]["comment"].to_s + "," + variable["comment"][index].to_s
    end   
  end

我正在尝试为Highcharts Demo - Basic bar生成数据。宝石是LazyHighCharts

有任何想法吗?建议?

编辑:

也许我必须再次解释一下结构:有问题 id(qid),每个问题都有一个值和一个注释,我试图计算“val”哈希的平均值

4

3 回答 3

0

好吧,我想我明白你在拍摄什么...

# lets create a hash to store the data
# such that my_data[qid][0] => value sum
#           my_data[qid][1] => number of times qid appeared
#           my_data[qid][2] => comments array

my_data = {}

# go through answers and fill my_data out

answers.each do |h|
    for i in 0...h["qid"].length
        qid = h["qid"][i]

        # awesome ruby syntax that will set my_data[qid] 
        # only if it hasn't already been set using "or-equals" operator
        my_data[qid] ||= [0, 0, []] 

        my_data[qid][0] += h["val"][i].to_i # add value
        my_data[qid][1] += 1                # increment count
        my_data[qid][2] << h["comment"][i]  # add comment
    end
 end

# how to get the data out for qid of "2"

my_data["2"][0].to_f / my_data["2"][1] # => 6
my_data["2"][2]                        # => ["super", "hmm"]

# and we could do this process for any qid, or all qids by iterating over
# my_data. we could even build the arrays in your OP

qids = []
average_values = []
comments = []

my_data.each do |k, v|
    qids << k
    average_values << v[0].to_f / v[1]
    comments << v[2]
end

# qids           => ["2", "1", "4", "5"]
# average_values => [6, 4.3, 7, 9]
# comments       => [["super", hmm"] ["", "supera", "super"], [""], ["asdf"]]
于 2013-05-14T22:13:38.660 回答
0

对于qid

result_qid = answers.map{|i| i['qid']}.flatten.uniq!
#["2", "1", "4", "5"]

result_qid = answers.map{|i| i['qid']}.flatten.group_by{|i| i}.map{|i,j| [i,j.length]}
#[["2", 2], ["1", 3], ["4", 1], ["5", 1]]

我无法弄清楚相应值的逻辑!

于 2013-05-15T01:42:27.437 回答
0

如果在 Hash 中存储数据受到如此限制,您可以定义一个新的 Class 并管理该 Class 中的数据处理。我会有这样的事情:

class AnswerParser
  def intialize(answers)
    #your implementation here
  end

  def qids
    #your implementation here
  end

  def comments
    #your implementation here
  end
  ...

end

#usage
parser = AnswerParser.new(anwsers)
qids = parser.qids
...

有了这个,您可以分离代码以便于测试和可用性。

希望这可以帮助。

于 2013-05-15T03:39:40.637 回答