我有CSV银行对帐单:
Transaction Date|Transaction Type|Transaction Description|Debit Amount
29/09/2015 DEB TFL.GOV.UK/CP 1.5
29/09/2015 DEB PRIMARK 674 14
29/09/2015 DEB PRIMARK 674 7
28/09/2016 DEB TFL 1.5
27/09/2016 DEB TFL 1.5
26/09/2016 DEB MCDONALDS 4.26
26/09/2016 DEB TFL 3
26/09/2016 DEB TFL 6.3
26/09/2017 CPT LNK SOUTHWARK 1 10
25/09/2017 DEB LIDL 19.3
我需要获得5个最高费用和交易名称。 示例McDonalds 4.26等......
到目前为止,我设法只获得了5个顶级项目。
需要'CSV'
list = []
CSV.foreach('statment_september.csv') do |row|
list << row[5].to_i
end
top_five = list.sort
top_five.last(5)
谢谢
答案 0 :(得分:1)
我会做这样的事情:
require 'csv'
top_five = CSV.read('statment_september.csv', headers: false)
.max_by(5) { |row| -row[3].to_f }