0

在 Ruby 中,我有一个如下所示的数组数组:

[["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 中实现这一目标的有效方法的想法?

4

4 回答 4

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"]]
于 2013-05-10T20:56:04.597 回答
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?} }
于 2013-05-10T20:25:02.090 回答
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
于 2013-05-10T20:11:19.070 回答
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 os 原始数组。

另一种方式:

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
于 2013-05-10T20:57:22.590 回答