簡體   English   中英

如何從哈希中提取數組值?

[英]How do I extract array values from hashes?

我在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欄生成數據。 寶石是LazyHighCharts

有任何想法嗎? 有什么建議嗎?

編輯:

也許我必須再次解釋其結構:有一個問題id(奇數),每個問題都有一個值和一個注釋,我試圖計算“ val”哈希的平均值

好吧,我想我知道您要射擊什么...

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

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

我不知道對應值的邏輯!

如果在哈希中存儲數據受到限制,則可以定義一個新的類並管理該類中的數據處理。 我會有這樣的事情:

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

借助此功能,您可以分離代碼以簡化測試和可用性。

希望這可以幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM