sort - ruby find item in array



如何計算Ruby數組中相同的字符串元素 (9)

實際上有一個這樣的數據結構: MultiSet

不幸的是,在Ruby核心庫或標準庫中沒有MultiSet實現,但是在Web上有幾個實現。

這是數據結構選擇如何簡化算法的一個很好的例子。 事實上,在這個特定的例子中,算法甚至完全消失。 這實際上只是:

Multiset.new(*names)

就是這樣。 例如,使用https://GitHub.Com/Josh/Multimap/

require 'multiset'

names = %w[Jason Jason Teresa Judah Michelle Judah Judah Allison]

histogram = Multiset.new(*names)
# => #<Multiset: {"Jason", "Jason", "Teresa", "Judah", "Judah", "Judah", "Michelle", "Allison"}>

histogram.multiplicity('Judah')
# => 3

例如,使用http://maraigue.hhiro.net/multiset/index-en.php

require 'multiset'

names = %w[Jason Jason Teresa Judah Michelle Judah Judah Allison]

histogram = Multiset[*names]
# => #<Multiset:#2 'Jason', #1 'Teresa', #3 'Judah', #1 'Michelle', #1 'Allison'>

我有以下Array = ["Jason", "Jason", "Teresa", "Judah", "Michelle", "Judah", "Judah", "Allison"]

我如何計算每個相同的元素

Where:
"Jason" = 2, "Judah" = 3, "Allison" = 1, "Teresa" = 1, "Michelle" = 1?

產生一個散列其中:

其中:hash = {“Jason”=> 2,“Judah”=> 3,“Allison”=> 1,“Teresa”=> 1,“Michelle”=> 1}


Answer #1
names = ["Jason", "Jason", "Teresa", "Judah", "Michelle", "Judah", "Judah", "Allison"]
counts = Hash.new(0)
names.each { |name| counts[name] += 1 }
# => {"Jason" => 2, "Teresa" => 1, ....

Answer #2

以下是一個稍微有些功能的編程風格:

array_with_lower_case_a = ["Jason", "Jason", "Teresa", "Judah", "Michelle", "Judah", "Judah", "Allison"]
hash_grouped_by_name = array_with_lower_case_a.group_by {|name| name}
hash_grouped_by_name.map{|name, names| [name, names.length]}
=> [["Jason", 2], ["Teresa", 1], ["Judah", 3], ["Michelle", 1], ["Allison", 1]]

group_by一個優點是你可以使用它來分組等效但不完全相同的項目:

another_array_with_lower_case_a = ["Jason", "jason", "Teresa", "Judah", "Michelle", "Judah Ben-Hur", "JUDAH", "Allison"]
hash_grouped_by_first_name = another_array_with_lower_case_a.group_by {|name| name.split(" ").first.capitalize}
hash_grouped_by_first_name.map{|first_name, names| [first_name, names.length]}
=> [["Jason", 2], ["Teresa", 1], ["Judah", 3], ["Michelle", 1], ["Allison", 1]]

Answer #3

這工作。

arr = ["Jason", "Jason", "Teresa", "Judah", "Michelle", "Judah", "Judah", "Allison"]
result = {}
arr.uniq.each{|element| result[element] = arr.count(element)}

Answer #4
names = ["Jason", "Jason", "Teresa", "Judah", "Michelle", "Judah", "Judah", "Allison"]
Hash[names.group_by{|i| i }.map{|k,v| [k,v.size]}]
# => {"Jason"=>2, "Teresa"=>1, "Judah"=>3, "Michelle"=>1, "Allison"=>1}

Answer #5
arr = ["Jason", "Jason", "Teresa", "Judah", "Michelle", "Judah", "Judah", "Allison"]

arr.uniq.inject({}) {|a, e| a.merge({e => arr.count(e)})}

經過的時間為0.028毫秒

有趣的是,stupidgeek的實施基準:

經過的時間0.041毫秒

和獲勝的答案:

經過的時間為0.011毫秒

:)


Answer #6

現在使用Ruby 2.2.0,你可以利用itself方法

names = ["Jason", "Jason", "Teresa", "Judah", "Michelle", "Judah", "Judah", "Allison"]
counts = {}
names.group_by(&:itself).each { |k,v| counts[k] = v.length }
# counts > {"Jason"=>2, "Teresa"=>1, "Judah"=>3, "Michelle"=>1, "Allison"=>1}

Answer #7

Enumberable#each_with_object返回最後一個散列。

names.each_with_object(Hash.new(0)) { |name, hash| hash[name] += 1}

返回:

=> {"Jason"=>2, "Teresa"=>1, "Judah"=>3, "Michelle"=>1, "Allison"=>1}

Answer #8

當這個問題首次被問到(2011年2月)時,下面的代碼在標準的ruby中是不可能的,因為它使用:

Ruby的這些現代附加功能使以下實現成為可能:

names = ["Jason", "Jason", "Teresa", "Judah", "Michelle", "Judah", "Judah", "Allison"]

names.group_by(&:itself).transform_values(&:count)
#=> {"Jason"=>2, "Teresa"=>1, "Judah"=>3, "Michelle"=>1, "Allison"=>1}




element