Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在ruby中从3D数组中删除元素_Ruby - Fatal编程技术网

如何在ruby中从3D数组中删除元素

如何在ruby中从3D数组中删除元素,ruby,Ruby,我试图从第1、第2和第5列中的每一维数组中删除第一个元素。 3D阵列看起来像 1st col 2nd col 3rd col 4th col 5th col [ [[0, 0, 0, 2, 3, 0, 0, 5], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0

我试图从第1、第2和第5列中的每一维数组中删除第一个元素。 3D阵列看起来像

       1st col              2nd col                       3rd col                  4th col                  5th col
[
[[0, 0, 0, 2, 3, 0, 0, 5], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0]], 
[[0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0]],
[[0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0]], 
[[0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0]],
[[0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0]],
[[0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0]], 
[[0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0]], 
[[0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0]]
]
所需的3D阵列应如下所示

       1st col              2nd col                       3rd col                  4th col                  5th col

[
[[ 0, 0, 2, 3, 0, 0, 5], [ 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [ 0, 0, 0, 0, 0, 0, 0]], 
[[ 0, 0, 0, 0, 0, 0, 0], [ 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [ 0, 0, 0, 0, 0, 0, 0]],
[[ 0, 0, 0, 0, 0, 0, 0], [ 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [ 0, 0, 0, 0, 0, 0, 0]], 
[[ 0, 0, 0, 0, 0, 0, 0], [ 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [ 0, 0, 0, 0, 0, 0, 0]],
[[ 0, 0, 0, 0, 0, 0, 0], [ 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [ 0, 0, 0, 0, 0, 0, 0]],
[[ 0, 0, 0, 0, 0, 0, 0], [ 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [ 0, 0, 0, 0, 0, 0, 0]], 
[[ 0, 0, 0, 0, 0, 0, 0], [ 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [ 0, 0, 0, 0, 0, 0, 0]], 
[[ 0, 0, 0, 0, 0, 0, 0], [ 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [ 0, 0, 0, 0, 0, 0, 0]]
]
我不确定用ruby做这件事的最佳方法是什么。

试试这个:

array.map do |row_of_column_arrays| 
  row_of_column_arrays.each_with_index.map do |column_arr, index| 
    [0, 1, 4].include?(index) ? column_arr[1..-1] : column_arr
  end
end
自下而上,阵列由以下维度组成:

1单元

2列数组,例如[0,0,0,2,3,0,0,5]

3行列数组,例如问题中的第一行

4列数组的行数组这是驻留在数组中的值

因此,第一个映射应用于4,并遍历列数组3的每一行。对于其中的每一个,我们运行第二个map方法,它遍历每个列数组2。如果我们处理对应于第1、第2和第5列的索引0、1和4,我们只需从该块返回不带第一个元素的特定列数组:

[0,0,0,2,3,0,0,5][1..-1]
# => [0,0,2,3,0,0,5]

如果我正确理解您的问题,您可以执行以下操作,其中arr是您的数组:

arr_cpy = Marshal.load(Marshal.dump(arr))
[0,1,4].each { |i| arr_cpy[i] = arr_cpy[i].transpose[1..-1].transpose }
arr_cpy

我已经使用和对arr做了一个比较,这样arr就不会被改变和变异。如果您希望改变arr,只需跳过该步骤,并将第二行中的arr_cpy更改为arr。

这是可行的,但它会从每一列中删除元素。我只需要删除第一、第二和第五列的第一个元素。谢谢阿米塔。这个解决方案非常有效。这也许不是选择答案的最佳理由。询问绿色皱褶。当你举一个例子时,你应该把它缩小到表达你观点所需的最小尺寸,例如,你能不能没有,比如说,4个4x4街区?;2将每个输入分配给一个变量,例如arr=[[0,0…]因此,读者可以在注释和答案中引用这些变量,而无需对其进行定义。如果我正确理解了您的问题,您要删除的所有元素都是零,这使得从示例中理解问题变得很困难。Cary Swoveland,我将确保下次简单回答我的问题。