【发布时间】:2019-09-18 21:28:27
【问题描述】:
迫切需要帮助。我正在尝试从数组中删除数组,并且遇到了障碍。本质上,如果子数组中的第一个值不存在于 任何其他 子数组的任一位置,则应将其删除。 (假设数组将被排序 - 因为它会)
arr = [[0, 1], [2, 3], [4, 5]]
arr.each_with_index do |inside_array, index|
if !index.zero?
# arr.delete(arr[index]) if arr.select {|x| x.include?(inside_array[0])}.count < 2
# refactored
arr.reject! {|x| x.include?(inside_array[0])}
end
end
=> [[0, 1], [4, 5]]
# Why does it stop itterating/enumerating after the first deletion?
# Goal output is [[0, 1]] for this example
类似地,[[0, 1], [2, 3], [1, 5]] 这样的数组应该产生[[0, 1], [1, 5]]
-要么 -
[[0, 1], [2, 3], [0, 3]],应该产生[[0, 1], [0, 3]]
【问题讨论】:
标签: arrays ruby methods enumeration