删除ruby中的重复数据

时间:2012-07-18 06:55:11

标签: ruby

我想基于所有三个字段从我的代码中删除重复项,即基于事件名称,日期和时间,如果所有三个字段都匹配两个事件,则应删除一个仅显示唯一事件。代码I已经为一个字段编写了作品,即根据event-name删除条目。如何匹配所有三个字段。我正在使用&但那没有成功。

include Enumerable

list = [
  {
     :eventname => "music show",
     :date => "1st august",
     :time => "9 pm"
  },
  {
      :eventname => "dance show",
      :date => "11st august",
      :time => "9 pm"
  },
  {
      :eventname => "music show",
      :date => "1st august",
      :time => "9 pm"
  },
  {
       :eventname => "music show",
       :date => "15st august",
       :time => "9 pm"
  },
  {
      :eventname => "magic show",
      :date => "12st august",
      :time => "9 pm"
  },
  {
      :eventname => "rock show",
      :date => "1st august",
      :time => "9 pm"
  }
]

b=list.group_by{|r| r[:eventname]}.map do |k, v|
    v.inject({}) { |r, h| r.merge(h){ |key, o, n| o || n } }
end

1 个答案:

答案 0 :(得分:4)

使用uniq的{​​{1}}方法:

Array