2013-05-10 101 views
0

在Ruby我有數組的數組,看起來像這樣的一個值,以斯馬什2D陣列:紅寶石:如何基於元素

[["2011-12-03 01:21:31", "Closed", ""], 
["2011-11-11 00:42:40", "", "Smith, Bob"], 
["2011-11-11 00:42:40", "Escalated", ""], 
["2011-11-10 21:36:11", "", "ABC Team"], 
["2011-11-10 21:36:11", "Escalated", ""], 
["2011-11-10 18:42:19", "", "Smith, Bob"], 
["2011-11-10 18:42:19", "Escalated", ""], 
["2011-11-09 22:55:01", "", "Global Design"], 
["2011-11-09 22:55:01", "Escalated", ""], 
["2011-09-28 18:56:32", "", "Jones, Fred"], 
["2011-09-28 18:56:32", "Escalated", ""], 
["2011-09-28 07:19:28", "", ""], 
["2011-09-28 07:19:28", "", ""], 
["2011-09-28 07:19:28", "", ""], 
["2011-09-28 07:19:28", "", ""], 
["2011-09-28 07:19:28", "", "Finance"], 
["2011-09-28 07:19:28", "Escalated", ""]] 

我想基於時間戳的條目斯馬什一起。我希望得到的陣列看起來像:

[["2011-12-03 01:21:31", "Closed", ""], 
["2011-11-11 00:42:40", "Escalated", "Smith, Bob"], 
["2011-11-10 21:36:11", "Escalated", "ABC Team"], 
["2011-11-10 18:42:19", "Escalated", "Smith, Bob"], 
["2011-11-09 22:55:01", "Escalated", "Global Design"], 
["2011-09-28 18:56:32", "Escalated", "Jones, Fred"], 
["2011-09-28 07:19:28", "Escalated", "Finance"]] 

任何有效的方式來實現這個在Ruby中的想法?

+0

可枚舉的GROUP_BY這裏是你的朋友。 – squiguy 2013-05-10 20:55:25

+0

你的問題不清楚。何時清除空字符串?它看起來是任意的。什麼決定了時間戳之後的元素的順序? – sawa 2013-05-10 21:23:28

回答

2

使用Enumerable#group_by,然後修復了分組值:

def smush_array(arr) 
    arr.group_by(&:first).map do |k,v| 
    row = [k, v.last[1]] + v[0..-1].sort.map(&:last).uniq 
    end.map {|x| (x.size > 3) ? x.reject(&:empty?) : x} 
end 

smushed = smush_array(array) 
# [["2011-12-03 01:21:31", "Closed", ""], 
# ["2011-11-11 00:42:40", "Escalated", "Smith, Bob"], 
# ["2011-11-10 21:36:11", "Escalated", "ABC Team"], 
# ["2011-11-10 18:42:19", "Escalated", "Smith, Bob"], 
# ["2011-11-09 22:55:01", "Escalated", "Global Design"], 
# ["2011-09-28 18:56:32", "Escalated", "Jones, Fred"], 
# ["2011-09-28 07:19:28", "Escalated", "Finance"]] 
0

我不知道這是否是最有效的方式做到這一點,但是這似乎是一個解決方案:

data = [["2011-12-03 01:21:31", "Closed", ""], 
["2011-11-11 00:42:40", "", "Smith, Bob"], 
["2011-11-11 00:42:40", "Escalated", ""], 
["2011-11-10 21:36:11", "", "ABC Team"], 
["2011-11-10 21:36:11", "Escalated", ""], 
["2011-11-10 18:42:19", "", "Smith, Bob"], 
["2011-11-10 18:42:19", "Escalated", ""], 
["2011-11-09 22:55:01", "", "Global Design"], 
["2011-11-09 22:55:01", "Escalated", ""], 
["2011-09-28 18:56:32", "", "Jones, Fred"], 
["2011-09-28 18:56:32", "Escalated", ""], 
["2011-09-28 07:19:28", "", ""], 
["2011-09-28 07:19:28", "", ""], 
["2011-09-28 07:19:28", "", ""], 
["2011-09-28 07:19:28", "", ""], 
["2011-09-28 07:19:28", "", "Finance"], 
["2011-09-28 07:19:28", "Escalated", ""]] 

map = {} 

data.each do |input| 
map[input[0]] ||= [] 
map[input[0]].concat(input[1..input.length].reject{|item| item.empty?}) 
end 

result = [] 
map.each do |k,v| 
    result << [k].concat(v) 
end 

puts result.to_s 
0
array = [ 
    ["2011-12-03 01:21:31", "Closed", ""], 
    ["2011-11-11 00:42:40", "", "Smith, Bob"], 
    ["2011-11-11 00:42:40", "Escalated", ""], 
    ["2011-11-10 21:36:11", "", "ABC Team"], 
    ["2011-11-10 21:36:11", "Escalated", ""], 
    ["2011-11-10 18:42:19", "", "Smith, Bob"], 
    ["2011-11-10 18:42:19", "Escalated", ""], 
    ["2011-11-09 22:55:01", "", "Global Design"], 
    ["2011-11-09 22:55:01", "Escalated", ""], 
    ["2011-09-28 18:56:32", "", "Jones, Fred"], 
    ["2011-09-28 18:56:32", "Escalated", ""], 
    ["2011-09-28 07:19:28", "", ""], 
    ["2011-09-28 07:19:28", "", ""], 
    ["2011-09-28 07:19:28", "", ""], 
    ["2011-09-28 07:19:28", "", ""], 
    ["2011-09-28 07:19:28", "", "Finance"], 
    ["2011-09-28 07:19:28", "Escalated", ""] 
] 

data = array.map {|e| e[0]}.uniq.map { |time| array.select {|e| e[0] == time}.flatten.uniq } 
puts data.to_s 

# output: 
# [ 
# ["2011-12-03 01:21:31", "Closed", ""] 
# ["2011-11-11 00:42:40", "", "Smith, Bob", "Escalated"] 
# ["2011-11-10 21:36:11", "", "ABC Team", "Escalated"] 
# ["2011-11-10 18:42:19", "", "Smith, Bob", "Escalated"] 
# ["2011-11-09 22:55:01", "", "Global Design", "Escalated"] 
# ["2011-09-28 18:56:32", "", "Jones, Fred", "Escalated"] 
# ["2011-09-28 07:19:28", "", "Finance", "Escalated"] 
# ] 

不知道如果你想要的空字符串不。如果沒有空字符串:

array.map {|e| e[0]}.uniq.map { |time| array.select {|e| e[0] == time}.flatten.uniq.reject{|e| e.empty?} } 
0
array.inject({}) {|h, a| e = h[a[0]] || [a[0]]; e[1] = e[1].nil? || e[1] == "" ? a[1] : e[1]; e[2] = e[2].nil? || e[2] == "" ? a[2] : e[2]; h[a[0]] = e; h }.values 
=> [["2011-12-03 01:21:31", "Closed", ""], 
["2011-11-11 00:42:40", "Escalated", "Smith, Bob"], 
["2011-11-10 21:36:11", "Escalated", "ABC Team"], 
["2011-11-10 18:42:19", "Escalated", "Smith, Bob"], 
["2011-11-09 22:55:01", "Escalated", "Global Design"], 
["2011-09-28 18:56:32", "Escalated", "Jones, Fred"], 
["2011-09-28 07:19:28", "Escalated", "Finance"]] 

其中陣列操作系統原始數組。

另一種方式:

array.each_with_object({}) {|a, h| e = (h[a[0]] ||= []); a.map.with_index {|x, i| e[i] = (e[i] && e[i] != "" ? e[i] : x) } }.values