如何解析文本文件并计算字符串出现的次数?

时间:2020-12-08 16:55:35

I have text file with a list of dates

我有一个日期列表的文本文件

2014-01-18
2014-01-18
2014-01-20
2014-01-20
2014-01-20
2014-01-21
2014-01-21
2014-01-22
2014-01-22
2014-01-22
2014-01-22
2014-01-22

How can I count how many times each date is recorded? So have an output something similar to:

我如何计算每个日期被记录的次数?输出类似于:

2014-01-18 2
2014-01-19 0
2014-01-20 3
2014-01-21 2
2014-01-22 5

3 个解决方案

#1


4  

path = '/path/to/file'
lines = File.readlines(path).map(&:chomp)

# At this point lines should look like below, this is just for testing
lines = ["2014-01-18", "2014-01-18", "2014-01-20", 
         "2014-01-20", "2014-01-20", "2014-01-21", 
         "2014-01-21", "2014-01-22", "2014-01-22", 
         "2014-01-22", "2014-01-22", "2014-01-22"]

# All Ruby versions (since you're using Ruby 1.9.3 you should use tihs)
Hash[ lines.group_by { |v| v }.map { |k, v| [k, v.size] } ]
# Ruby >= 2.1.0
lines.group_by { |v| v }.map { |k, v| [k, v.size] }.to_h

#=> {"2014-01-18"=>2, "2014-01-20"=>3, "2014-01-21"=>2, "2014-01-22"=>5}

#2


3  

I like using Hash.new

我喜欢用Hash.new

lines = ["2014-01-18", "2014-01-18", "2014-01-20", 
         "2014-01-20", "2014-01-20", "2014-01-21", 
         "2014-01-21", "2014-01-22", "2014-01-22", 
         "2014-01-22", "2014-01-22", "2014-01-22"]

result = Hash.new(0)
lines.each { |line| result[line] += 1 }

result
# => {"2014-01-18"=>2, "2014-01-20"=>3, "2014-01-21"=>2, "2014-01-22"=>5}

#3


2  

lines = File.readlines('file.txt').map(&:chomp)

op = Hash.new(0)

lines.each do |line|
  op[line.to_sym] += 1
end

puts op.sort_by { |k, v| v }

#1


4  

path = '/path/to/file'
lines = File.readlines(path).map(&:chomp)

# At this point lines should look like below, this is just for testing
lines = ["2014-01-18", "2014-01-18", "2014-01-20", 
         "2014-01-20", "2014-01-20", "2014-01-21", 
         "2014-01-21", "2014-01-22", "2014-01-22", 
         "2014-01-22", "2014-01-22", "2014-01-22"]

# All Ruby versions (since you're using Ruby 1.9.3 you should use tihs)
Hash[ lines.group_by { |v| v }.map { |k, v| [k, v.size] } ]
# Ruby >= 2.1.0
lines.group_by { |v| v }.map { |k, v| [k, v.size] }.to_h

#=> {"2014-01-18"=>2, "2014-01-20"=>3, "2014-01-21"=>2, "2014-01-22"=>5}

#2


3  

I like using Hash.new

我喜欢用Hash.new

lines = ["2014-01-18", "2014-01-18", "2014-01-20", 
         "2014-01-20", "2014-01-20", "2014-01-21", 
         "2014-01-21", "2014-01-22", "2014-01-22", 
         "2014-01-22", "2014-01-22", "2014-01-22"]

result = Hash.new(0)
lines.each { |line| result[line] += 1 }

result
# => {"2014-01-18"=>2, "2014-01-20"=>3, "2014-01-21"=>2, "2014-01-22"=>5}

#3


2  

lines = File.readlines('file.txt').map(&:chomp)

op = Hash.new(0)

lines.each do |line|
  op[line.to_sym] += 1
end

puts op.sort_by { |k, v| v }