Ruby复杂字符串数组到浮点数组

cgfeq70w  于 12个月前  发布在  Ruby
关注(0)|答案(3)|浏览(83)

我试图将这个(相当复杂的)float字符串数组转换为一个float数组,但没有成功:

[  5.85142857   6.10684807   6.15328798   6.31582766   6.96598639
   7.61614512   7.87156463   8.73070295   9.38086168   9.65950113
  10.51863946  12.07437642  12.32979592  13.39791383  13.63011338
  13.86231293  14.09451247  14.72145125  14.97687075  15.85922902 ]

我所尝试的:

def onset_string_to_array(onset)
    # Remove the brackets.
    onset = onset.tr('[]', '')
    # Strip whitespace.
    onset = onset.strip
    # Split by newline character.
    onset_lines = onset.split("\n")
    # loop through each line.
    onset_array = []
    onset_array.push *onset_lines.map do |line|
      # Split by space character.
      line_array = line.split
      # Return one float per line.
      line_array.map(&:to_f)
    end
    onset_array
  end

给了我这个:

[
  "0.53405896   0.7662585    0.99845805   1.20743764   1.41641723",
  "   1.64861678   1.85759637   2.32199546   3.43655329   3.57587302",
  "   4.08671202   4.7600907    4.99229025   5.87464853   6.52480726",
  "   6.78022676   7.66258503   8.33596372   9.4737415   10.12390023",
  "  10.35609977  10.750839    11.2152381   11.88861678  12.14403628",
  "  12.60843537  12.84063492  13.04961451  13.69977324  13.95519274",
  "  14.11773243  14.58213152  14.79111111  15.4644898   15.69668934",
  "  16.60226757  17.27564626  17.5078458   18.39020408  19.06358277",
  ## More lines...
  " 126.54875283 127.45433107 129.03328798 129.21904762 130.77478458",
  " 131.00698413 131.86612245 132.81814059 133.35219955 134.55963719",
  " 135.14013605"
]

我想得到的:

[ 0.53405896, 0.7662585, 0.99845805, 1.20743764, 1.41641723, 1.64861678, ...]

有线索吗?

llew8vvj

llew8vvj1#

要解析此字符串,您可以:

onset.delete('[]').split.map(&:to_f)

(1)首先删除括号,(2)然后将字符串按空格拆分为子字符串,(3)最后转换为浮点数

onset =
  '[  5.85142857   6.10684807   6.15328798   6.31582766   6.96598639
      7.61614512   7.87156463   8.73070295   9.38086168   9.65950113
     10.51863946  12.07437642  12.32979592  13.39791383  13.63011338
     13.86231293  14.09451247  14.72145125  14.97687075  15.85922902 ]'

onset.delete('[]').split.map(&:to_f)
# => 
# [5.85142857,
#  6.10684807,
#  6.15328798,
#  6.31582766,
#  6.96598639,
#  7.61614512,
#  7.87156463,
#  8.73070295,
#  9.38086168,
#  9.65950113,
#  10.51863946,
#  12.07437642,
#  12.32979592,
#  13.39791383,
#  13.63011338,
#  13.86231293,
#  14.09451247,
#  14.72145125,
#  14.97687075,
#  15.85922902]

顺便说一句,Ruby有内置的方法从字符串中获取行,你不需要用新的行字符分割它,只需使用string.lines

siotufzp

siotufzp2#

我猜

str = "[  5.85142857   6.10684807   6.15328798   6.31582766   6.96598639\n  7.61614512   7.87156463   8.73070295   9.38086168   9.65950113\n  10.51863946  12.07437642  12.32979592  13.39791383  13.63011338\n  13.86231293  14.09451247  14.72145125  14.97687075  15.85922902 ]".

在这种情况下,你可以写

str.lines.map { |line| line.scan(/\d+\.\d+/).map(&:to_f) }
  #=> [[ 5.85142857,  6.10684807,  6.15328798,  6.31582766,  6.96598639],
  #    [ 7.61614512,  7.87156463,  8.73070295,  9.38086168,  9.65950113],
  #    [10.51863946, 12.07437642, 12.32979592, 13.39791383, 13.63011338],
  #    [13.86231293, 14.09451247, 14.72145125, 14.97687075, 15.85922902]]

例如,

line = "10.51863946, 12.07437642, 12.32979592, 13.39791383, 13.63011338"

然后

arr = line.scan(/\d+\.\d+/)
  #=> ["10.51863946", "12.07437642", "12.32979592", "13.39791383", "13.63011338"]
arr.map(&:to_f)
  #=> [10.51863946, 12.07437642, 12.32979592, 13.39791383, 13.63011338]

如果“flatten”的意思是希望生成一个包含20个浮点数的数组,而不是一个包含4个数组(每个数组包含5个浮点数)的数组(如上所述),那么可以这样写:

str.scan(/\d+\.\d+/).map(&:to_f)
  #=> [ 5.85142857,  6.10684807,  6.15328798,  6.31582766,  6.96598639,
  #     7.61614512,  7.87156463,  8.73070295,  9.38086168,  9.65950113,
  #    10.51863946, 12.07437642, 12.32979592, 13.39791383, 13.63011338,
  #    13.86231293, 14.09451247, 14.72145125, 14.97687075, 15.85922902]
a5g8bdjr

a5g8bdjr3#

您已经去掉了[],并去掉了前导/尾随空格,但在此之后,您就走上了一条不必要的复杂道路。
只需调用split,默认情况下它会将空白字符串作为其字符串进行匹配:

str = '[  5.85142857   6.10684807   6.15328798   6.31582766   6.96598639
   7.61614512   7.87156463   8.73070295   9.38086168   9.65950113
  10.51863946  12.07437642  12.32979592  13.39791383  13.63011338
  13.86231293  14.09451247  14.72145125  14.97687075  15.85922902 ]'

str.tr('[]', '').strip.split.map(&:to_f)

至于代码失败的原因,您将一个数组的数组溅射到初始数组上,这意味着您从未真正展平它。
如果出于某种原因,你不得不避免用正则表达式来拆分整个字符串,而是逐行拆分,你应该跳过array.push(*other_array.map),只使用flat_map

str = '5.85142857   6.10684807   6.15328798   6.31582766   6.96598639
   7.61614512   7.87156463   8.73070295   9.38086168   9.65950113
  10.51863946  12.07437642  12.32979592  13.39791383  13.63011338
  13.86231293  14.09451247  14.72145125  14.97687075  15.85922902'

str.lines.flat_map { |line| line.split.map(&:to_f) }

任何一个选项都将产生你所追求的浮动的平面列表:

[5.85142857,
 6.10684807,
 6.15328798,
 6.31582766,
 6.96598639,
 7.61614512,
 7.87156463,
 8.73070295,
 9.38086168,
 9.65950113,
 10.51863946,
 12.07437642,
 12.32979592,
 13.39791383,
 13.63011338,
 13.86231293,
 14.09451247,
 14.72145125,
 14.97687075,
 15.85922902]

相关问题