在给定的索引处将数组切成两半,但省略索引

时间:2022-03-08 21:35:11

Given an array

给一个数组

x = [2, 3, 4, 5, 6, 7]

I'd like to slice it in half at a certain point/index and create two subarrays of such cut, but leaving out the index. Example:

我想在某个点/索引处将它切成两半,然后创建两个这样的子数组,但省略了索引。例子:

x = [2, 3, 4, 6, 70, 10]
left, right = x.slice_at_index(2)
left  = [2, 3]
right = [6, 70, 10]

I've tried with each_index, slice, chunk but can't leave out the index element.

我尝试过使用each_index、slice、chunk,但不能忽略index元素。

5 个解决方案

#1


3  

left, right = x.take(index), x.drop(index.next)

#2


1  

left = x[0..1]
right = x[3..-1]

#3


1  

I think, docs like about array should help you next time.

我想,关于数组的文档应该会在下次帮助你。

So, UPD solution for you:

所以,UPD解决方案:

x = [2, 3, 4, 6, 70, 10]
left, right = x.shift(2), x.drop(2.pred)

> left
=> [2, 3]
> right
=> [6, 70, 10] 

#4


0  

You can write a method:

你可以写一个方法:

def slice_at_index(array, index)
  left = array[0...index]  # 3 dots vs 2
  right = array[(index + 1)..-1]
  return [left, right]
end

But, I would generally avoid multiple assignment such as left, right = slice_at_index(x, 2) as a style rule.

但是,作为一种样式规则,我通常会避免多次赋值,比如左、右= slice_at_index(x, 2)。

#5


0  

The easiest way, and I think most readable, is to use the range operator .. to specify the indexes you want out of the array.

最简单的方法,也是我认为最易读的方法,就是使用范围运算符。指定要从数组中取出的索引。

def slice_at(array, index)
  left = array[0..(index - 1)]
  right = array[(index + 1)..-1]
  return [left, right]
end

If you'd like to learn more about how the range operator works when used this way, read this post.

如果您想了解更多关于range操作符如何使用的信息,请阅读本文。

Remember that you can use ... or .. in Ruby to generate ranges. Using three dots will include the starting point, but exclude the ending point, while using two dots will include both the starting and ending points. See more here.

记住你可以用……或. .在Ruby中生成范围。使用三个点将包含起始点,但排除结束点,同时使用两个点将包括起始点和结束点。看到更多。

#1


3  

left, right = x.take(index), x.drop(index.next)

#2


1  

left = x[0..1]
right = x[3..-1]

#3


1  

I think, docs like about array should help you next time.

我想,关于数组的文档应该会在下次帮助你。

So, UPD solution for you:

所以,UPD解决方案:

x = [2, 3, 4, 6, 70, 10]
left, right = x.shift(2), x.drop(2.pred)

> left
=> [2, 3]
> right
=> [6, 70, 10] 

#4


0  

You can write a method:

你可以写一个方法:

def slice_at_index(array, index)
  left = array[0...index]  # 3 dots vs 2
  right = array[(index + 1)..-1]
  return [left, right]
end

But, I would generally avoid multiple assignment such as left, right = slice_at_index(x, 2) as a style rule.

但是,作为一种样式规则,我通常会避免多次赋值,比如左、右= slice_at_index(x, 2)。

#5


0  

The easiest way, and I think most readable, is to use the range operator .. to specify the indexes you want out of the array.

最简单的方法,也是我认为最易读的方法,就是使用范围运算符。指定要从数组中取出的索引。

def slice_at(array, index)
  left = array[0..(index - 1)]
  right = array[(index + 1)..-1]
  return [left, right]
end

If you'd like to learn more about how the range operator works when used this way, read this post.

如果您想了解更多关于range操作符如何使用的信息,请阅读本文。

Remember that you can use ... or .. in Ruby to generate ranges. Using three dots will include the starting point, but exclude the ending point, while using two dots will include both the starting and ending points. See more here.

记住你可以用……或. .在Ruby中生成范围。使用三个点将包含起始点,但排除结束点,同时使用两个点将包括起始点和结束点。看到更多。