我有一个CSV数据文件,其中的行可能有很多列500+,有些则很少。我需要转置它,以便每行成为输出文件中的一列。的问题是,在原始文件中的行可以不都具有相同的列数,所以当我尝试阵列的转置方法获得:使用FasterCSV将不均匀的行转换为列
`转置':元件尺寸不同(12应为5) (IndexError)
是否有替代转置与数组长度不一致?
我有一个CSV数据文件,其中的行可能有很多列500+,有些则很少。我需要转置它,以便每行成为输出文件中的一列。的问题是,在原始文件中的行可以不都具有相同的列数,所以当我尝试阵列的转置方法获得:使用FasterCSV将不均匀的行转换为列
`转置':元件尺寸不同(12应为5) (IndexError)
是否有替代转置与数组长度不一致?
我会插入空值来填充你的矩阵的孔中,一些诸如:
a = [[1, 2, 3], [3, 4]]
# This would throw the error you're talking about
# a.transpose
# Largest row
size = a.max { |r1, r2| r1.size <=> r2.size }.size
# Enlarge matrix inserting nils as needed
a.each { |r| r[size - 1] ||= nil }
# So now a == [[1, 2, 3], [3, 4, nil]]
aa = a.transpose
# aa == [[1, 3], [2, 4], [3, nil]]
结束使用此解决方案。谢谢 – srboisvert 2008-11-02 12:44:09
# Intitial CSV table data
csv_data = [ [1,2,3,4,5], [10,20,30,40], [100,200] ]
# Finding max length of rows
row_length = csv_data.map(&:length).max
# Inserting nil to the end of each row
csv_data.map do |row|
(row_length - row.length).times { row.insert(-1, nil) }
end
# Let's check
csv_data
# => [[1, 2, 3, 4, 5], [10, 20, 30, 40, nil], [100, 200, nil, nil, nil]]
# Transposing...
transposed_csv_data = csv_data.transpose
# Hooray!
# => [[1, 10, 100], [2, 20, 200], [3, 30, nil], [4, 40, nil], [5, nil, nil]]
人,那是甜蜜的是红宝石已转置内置阵列。我通常会写一个脚本来用其他语言来完成它。 – wprl 2008-10-30 16:25:39