2014-03-26 85 views
0

既然我有兩個屬性,一個數組對象集查找陣列具有相同屬性的

my_arr = [{n_parents: 10, class: 'right'}, {n_parents: 10, class: 'right'}, {n_parents: 5, class: 'left'}, {n_parents: 2, class: 'center'}, {n_parents: 2, class: 'center'}, {n_parents: 2, class: 'center'}] 

我想獲得與對象的數組分享這兩個屬性中的大部分。所以在前面的例子:

result = [{n_parents: 2, class: 'center'}, {n_parents: 2, class: 'center'}, {n_parents: 2, class: 'center'}] 

因爲是共享n_parents = 2,並class = 'center'三個對象。

到目前爲止,我知道如何根據dos兩個屬性進行分組,但之後我不確定如何獲取具有更多元素的集合。

現在我有:

my_arr.group_by { |x| [x[:n_parents], x[:class]] } 

回答

2

這應該爲你工作。這組散列由散列本身,然後由陣列得到最大的組數像下面

my_arr = [{n_parents: 10, class: 'right'}, {n_parents: 10, class: 'right'}, {n_parents: 5, class: 'left'}, {n_parents: 2, class: 'center'}, {n_parents: 2, class: 'center'}, {n_parents: 2, class: 'center'}] 
my_arr.group_by { |h| h }.max_by { |h,v| v.count }.last 
#=>[{:n_parents=>2, :class=>"center"}, {:n_parents=>2, :class=>"center"}, {:n_parents=>2, :class=>"center"}] 
+0

幽祕在結尾處發現'#last'。所以我想,沒有辦法,你是如何得到輸出的;;) –

+1

@ArupRakshit我注意到並添加它爲什麼你需要添加空格?明晰? – engineersmnky

+0

當然,就像Ruby愛好者喜歡那種寫法一樣。見[這裏](http://ruby-doc.org/core-2.1.0/Enumerable.html#method-i-group_by).. –

0

東西:

my_arr.group_by(&:values).max_by { |_,v| v.size }.last 
# => [{:n_parents=>2, :class=>"center"}, 
#  {:n_parents=>2, :class=>"center"}, 
#  {:n_parents=>2, :class=>"center"}] 
0

我使用由OP使用的代碼和擴展了,要得到結果他想: -

my_arr.group_by { |x| [x[:n_parents], x[:class]] }.max_by{|k,v| v.size}.last 

輸出

#=> [{:n_parents=>2, :class=>"center"}, {:n_parents=>2, :class=>"center"}, {:n_parents=>2, :class=>"center"}] 
0

這是要發佈的第四個答案。前三個答案全部採用group_by/max_by/last。當然,這可能是最好的方法,但它是最有趣,最有趣的嗎?以下是其他一些方法來生成所需的結果。當

my_arr = [{n_parents: 10, class: 'right' }, {n_parents: 10, class: 'right' }, 
      {n_parents: 5, class: 'left' }, {n_parents: 2, class: 'center'}, 
      {n_parents: 2, class: 'center'}, {n_parents: 2, class: 'center'}] 

期望的結果是:

#=> [{:n_parents=>2, :class=>"center"}, 
    # {:n_parents=>2, :class=>"center"}, 
    # {:n_parents=>2, :class=>"center"}] 

#1

# Create a hash `g` whose keys are the elements of `my_arr` (hashes) 
# and whose values are counts for the elements of `my_arr`. 
# `max_by` the values (counts) and construct the array. 

el, nbr = my_arr.each_with_object({}) { |h,g| g[h] = (g[h] ||= 0) + 1 } 
       .max_by { |_,v| v } 
arr = [el]*nbr 

#2

# Sequentially delete the elements equal to the first element of `arr`, 
# each time calculating the number of elements deleted, by determining 
# `arr.size` before and after the deletion. Compare that number with the 
# largest number deleted so far to find the element with the maximum 
# number of instances in `arr`, then construct the array. 

arr = my_arr.map(&:dup) 
most_plentiful = { nbr_copies: 0, element: [] } 
until arr.empty? do 
    sz = arr.size 
    element = arr.delete(arr.first) 
    if sz - arr.size > most_plentiful[:nbr_copies] 
    most_plentiful = { nbr_copies: sz - arr.size, element: element } 
    end 
end 
arr = [most_plentiful[:element]]* most_plentiful[:nbr_copies] 
+0

是的,你的代碼當然可以工作,並且是一個有趣的方法,但同時它會被重構,因爲它缺乏其他答案的簡潔性和可讀性。 – engineersmnky

+0

@ engineersmnky,我同意100%。如果這是真實的,我會像其他人一樣使用group_by/max_by。然而,我從Ruby中學到了很多東西,試圖想出針對SO問題的不同的,有時是非傳統的解決方案,特別是當我遲到派對時,就像這裏一樣。我張貼這些沉思,希望他們可能有一些教育價值。嘗試一下! –

相關問題