使用 each_with_slice 拆分数组但在结果数组中保留特定值(一个衬里)
Splitting an array using each_with_slice but keeping specific value(s) in resultant arrays (one liner)
我希望将一个数组转换成一种特定的格式,并尽可能在一行中进行。使用 each_slice(2).to_a
有助于拆分数组,但是我想将每个数组的第二个坐标保留为下一个数组的第一个坐标。
初始数组
# format is [x1, y1, x2, y2, x3, y3, x4, y4]...
full_line_coords = [[1, 1, 2, 2, 3, 3, 4, 4], [5, 5, 6, 6, 7, 7, 8, 8]]
期望输出
# desired format is [[[x1, y1], [x2, y2]], [[x2, y2], [x3, y3]], [[x3, y3], [x4, y4]]]...
desired = [[[1, 1], [2, 2]], [[2, 2], [3, 3]], [[3, 3], [4, 4]]]
不单单成功
# without one line:
desired = []
temp_array = []
full_line_coords.each do |x|
temp_array << x.each_slice(2).to_a
end
temp_array.each do |x|
i = 0
until i == x.length - 1
desired << [x[i], x[i+1]]
i += 1
end
end
p desired
# => [[[1, 1], [2, 2]], [[2, 2], [3, 3]], [[3, 3], [4, 4]], [[5, 5], [6, 6]], [[6, 6], [7, 7]], [[7, 7], [8, 8]]]
不确定如何将其设为单行,发现进行拆分很简单,但没有在每个数组中保留 end/start 坐标(如下所示)。
一线尝试
attempt = full_line_coords.each { |x| p x.each_slice(2).to_a.each_slice(2).to_a } # p to show this is where i'd like the array to be in 'desired' format if possible.
# => [[[1, 1], [2, 2]], [[3, 3], [4, 4]]]
# [[[5, 5], [6, 6]], [[7, 7], [8, 8]]]
Background/Reasoning
我希望保留它的唯一原因是单行是因为我想return“父”对象本身,而不仅仅是结果属性。
“父”对象是 link 的列表:#<Link:0x803a2e8>
,具有许多属性,包括“段”。
links.each do |l|
puts l.segments
end
# Gives an array of XY coordinates, including all vertices. e.g. [1, 1, 2, 2, 3, 3, 4, 4]
然后我希望在其他一些定义的方法中使用“所需”输出,但 return link 对象本身位于 #<Link:0x803a2e8>
末尾,而不仅仅是来自 link的属性。
非常感谢。
输入
full_line_coords = [[1, 1, 2, 2, 3, 3, 4, 4], [5, 5, 6, 6, 7, 7, 8, 8]]
代码
p full_line_coords.map { |var| var.slice_when { |x, y| x != y }
.each_cons(2)
.to_enum
.map(&:itself) }
输出
[[[[1, 1], [2, 2]], [[2, 2], [3, 3]], [[3, 3], [4, 4]]], [[[5, 5], [6, 6]], [[6, 6], [7, 7]], [[7, 7], [8, 8]]]]
这是我找到的第一个选项:
full_line_coords.flat_map { |e| e.each_slice(2).each_cons(2).to_a }
找到Enumerable class and Arrayclass
中的方法
我希望将一个数组转换成一种特定的格式,并尽可能在一行中进行。使用 each_slice(2).to_a
有助于拆分数组,但是我想将每个数组的第二个坐标保留为下一个数组的第一个坐标。
初始数组
# format is [x1, y1, x2, y2, x3, y3, x4, y4]...
full_line_coords = [[1, 1, 2, 2, 3, 3, 4, 4], [5, 5, 6, 6, 7, 7, 8, 8]]
期望输出
# desired format is [[[x1, y1], [x2, y2]], [[x2, y2], [x3, y3]], [[x3, y3], [x4, y4]]]...
desired = [[[1, 1], [2, 2]], [[2, 2], [3, 3]], [[3, 3], [4, 4]]]
不单单成功
# without one line:
desired = []
temp_array = []
full_line_coords.each do |x|
temp_array << x.each_slice(2).to_a
end
temp_array.each do |x|
i = 0
until i == x.length - 1
desired << [x[i], x[i+1]]
i += 1
end
end
p desired
# => [[[1, 1], [2, 2]], [[2, 2], [3, 3]], [[3, 3], [4, 4]], [[5, 5], [6, 6]], [[6, 6], [7, 7]], [[7, 7], [8, 8]]]
不确定如何将其设为单行,发现进行拆分很简单,但没有在每个数组中保留 end/start 坐标(如下所示)。
一线尝试
attempt = full_line_coords.each { |x| p x.each_slice(2).to_a.each_slice(2).to_a } # p to show this is where i'd like the array to be in 'desired' format if possible.
# => [[[1, 1], [2, 2]], [[3, 3], [4, 4]]]
# [[[5, 5], [6, 6]], [[7, 7], [8, 8]]]
Background/Reasoning
我希望保留它的唯一原因是单行是因为我想return“父”对象本身,而不仅仅是结果属性。
“父”对象是 link 的列表:#<Link:0x803a2e8>
,具有许多属性,包括“段”。
links.each do |l|
puts l.segments
end
# Gives an array of XY coordinates, including all vertices. e.g. [1, 1, 2, 2, 3, 3, 4, 4]
然后我希望在其他一些定义的方法中使用“所需”输出,但 return link 对象本身位于 #<Link:0x803a2e8>
末尾,而不仅仅是来自 link的属性。
非常感谢。
输入
full_line_coords = [[1, 1, 2, 2, 3, 3, 4, 4], [5, 5, 6, 6, 7, 7, 8, 8]]
代码
p full_line_coords.map { |var| var.slice_when { |x, y| x != y }
.each_cons(2)
.to_enum
.map(&:itself) }
输出
[[[[1, 1], [2, 2]], [[2, 2], [3, 3]], [[3, 3], [4, 4]]], [[[5, 5], [6, 6]], [[6, 6], [7, 7]], [[7, 7], [8, 8]]]]
这是我找到的第一个选项:
full_line_coords.flat_map { |e| e.each_slice(2).each_cons(2).to_a }
找到Enumerable class and Arrayclass
中的方法