我有一个生成随机电子邮件地址的函数:
def emails
names = ["alfred", "daniel", "elisa", "ana", "ramzes"]
surnames = ["oak", "leaf", "grass", "fruit"]
providers = ["gmail", "yahoo", "outlook", "icloud"]
address = "#{names.sample}.#{surnames.sample}#{rand(100..5300)}@#{providers.sample}.com"
end
给定随机生成的电子邮件地址列表:
email_list = 100.times.map { emails }
看起来像这样:
<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="82e6e3ecebe7eeacede3e9b1bbbab7c2ebe1eeedf7e6ace1edef" rel="noreferrer noopener nofollow">[email protected]</a>
<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="ea988b87908f99c48d988b9999dbdbdcdcaa838986859f8ec4898587" rel="noreferrer noopener nofollow">[email protected]</a>
<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="472326292e222b692135322e337e7e75073e262f28286924282a" rel="noreferrer noopener nofollow">[email protected]</a>
...
如何选择最常见的提供商(“gmail”、“yahoo”等)?
最佳答案
您的问题与此类似one 。但有一个不同之处:您不想分析电子邮件地址的频率,而是分析它们的提供商。
def random_email
names = ["alfred", "daniel", "elisa", "ana", "ramzes"]
surnames = ["oak", "leaf", "grass", "fruit"]
providers = ["gmail", "yahoo", "outlook", "icloud"]
address = "#{names.sample}.#{surnames.sample}#{rand(100..5300)}@#{providers.sample}.com"
end
emails = Array.new(100){ random_email }
freq = emails.each_with_object(Hash.new(0)) do |email,freq|
provider = email.split('@').last
freq[provider] += 1
end
p freq
#=> {"outlook.com"=>24, "yahoo.com"=>28, "gmail.com"=>32, "icloud.com"=>16}
p freq.max_by{|provider, count| count}.first
#=> "gmail.com"
关于ruby - 从电子邮件地址列表中选择最常见的域,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41893714/