0

我有一个特定的问题,我找不到解决方案。我从文本文件中获取以下格式的数据

    date1 reason1
    date1 reason1
    date1 reason2
    date1 reason3
    date2 reason4
    date2 reason1
    date2 reason2
    date2 reason2
    date2 reason1
    date2 reason3
    date3 reason4
    date3 reason4
    date3 reason1

我想建立关于数据的统计数据,例如我想像这样计算每个日期的所有不同“原因”

    date1 reason1 -> 2        
    date1 reason2 -> 1
    date1 reason3 -> 1
    date2 reason1 -> 2
    date2 reason4 -> 1
    date2 reason2 -> 2
    date1 reason3 -> 1

...等等。我将如何解析数据并构建所需的结果?我猜会使用哈希,但我想不出解决这个问题的方法。

4

3 回答 3

2

这是一个非常直接的方法:

h = Hash.new(0)
File.foreach("foo.txt") do |line|
  h[line.chomp] += 1  
end  
h
#=> {"date1 reason1"=>2,
 "date1 reason2"=>1,
 "date1 reason3"=>1,
 "date2 reason4"=>1,
 "date2 reason1"=>2,
 "date2 reason2"=>2,
 "date2 reason3"=>1,
 "date3 reason4"=>2,
 "date3 reason1"=>1}
于 2012-07-07T12:40:24.580 回答
1
str = "date1 reason1
date1 reason1
date1 reason2
date1 reason3
date2 reason4
date2 reason1
date2 reason2
date2 reason2
date2 reason1
date2 reason3
date3 reason4
date3 reason4
date3 reason1"

line_counts = Hash.new(0)

str.lines.each do |line|
  line_counts[line.chomp] += 1
end

line_counts.each do |line, count|
  puts "#{line} -> #{count}"
end

输出:

date1 reason1 -> 2
date1 reason2 -> 1
date1 reason3 -> 1
date2 reason1 -> 2
date2 reason2 -> 2
date2 reason3 -> 1
date2 reason4 -> 1
date3 reason1 -> 1
date3 reason4 -> 2
于 2012-07-07T12:47:10.547 回答
0
result = File.foreach("foo.txt").each_with_object(Hash.new(0)) do |line,h|
  h[line.chomp] += 1  
end  
#=> {"date1 reason1"=>2,
 "date1 reason2"=>1,
 "date1 reason3"=>1,
 "date2 reason4"=>1,
 "date2 reason1"=>2,
 "date2 reason2"=>2,
 "date2 reason3"=>1,
 "date3 reason4"=>2,
 "date3 reason1"=>1}
于 2012-07-07T13:30:17.540 回答