Lets say I have an array
假设有一个数组。
[0, 132, 432, 342, 234]
What is the easiest way to get rid of the first element? (0)
去掉第一个元素最简单的方法是什么?(0)
11 个解决方案
#1
237
"pop"ing the first element of an Array is called "shift" ("unshift" being the operation of adding one element in front of the array).
“pop”数组的第一个元素被称为“shift”(“unshift”是在数组前面添加一个元素的操作)。
#2
286
Use the shift
method on array
在数组上使用shift方法。
>> x = [4,5,6]
=> [4, 5, 6]
>> x.shift
=> 4
>> x
=> [5, 6]
If you want to remove n starting elements you can use x.shift(n)
如果你想移除n个起始元素,你可以使用x。shift(n)
#3
284
a = [0,1,2,3]
a.drop(1)
# => [1, 2, 3]
a
# => [0,1,2,3]
and additionally:
另外:
[0,1,2,3].drop(2)
=> [2, 3]
[0,1,2,3].drop(3)
=> [3]
#4
116
[0, 132, 432, 342, 234][1..-1]
=> [132, 432, 342, 234]
So unlike shift
or slice
this returns the modified array (useful for one liners).
因此,与shift或slice不同的是,它返回了修改后的数组(对于一个内层来说是有用的)。
#5
87
This is pretty neat:
这是非常整洁:
head, *tail = [1, 2, 3, 4, 5]
#==> head = 1, tail = [2, 3, 4, 5]
As written in the comments, there's an advantage of not mutating the original list.
正如在评论中所写的,没有改变原始列表是有好处的。
#6
18
or a.delete_at 0
或a.delete_at 0
#7
11
Use shift method
使用转换方法
array.shift(n) => Remove first n elements from array
array.shift(1) => Remove first element
https://ruby-doc.org/core-2.2.0/Array.html#method-i-shift
https://ruby-doc.org/core-2.2.0/Array.html method-i-shift
#9
4
You can use Array.delete_at(0) method which will delete first element.
您可以使用Array.delete_at(0)方法来删除第一个元素。
x = [2,3,4,11,0]
x.delete_at(0) unless x.empty? # [3,4,11,0]
#10
2
You can use:arr - [arr[0]]
or else arr - [arr.shift()]
or simply arr.shift(1)
您可以使用:arr - [arr[0]]或其他arr - [arr.shift()]或简单的arr.shift(1)
#11
0
You can use:
您可以使用:
a.delete(a[0])
a.delete_at 0
Both can work
都可以工作
#1
237
"pop"ing the first element of an Array is called "shift" ("unshift" being the operation of adding one element in front of the array).
“pop”数组的第一个元素被称为“shift”(“unshift”是在数组前面添加一个元素的操作)。
#2
286
Use the shift
method on array
在数组上使用shift方法。
>> x = [4,5,6]
=> [4, 5, 6]
>> x.shift
=> 4
>> x
=> [5, 6]
If you want to remove n starting elements you can use x.shift(n)
如果你想移除n个起始元素,你可以使用x。shift(n)
#3
284
a = [0,1,2,3]
a.drop(1)
# => [1, 2, 3]
a
# => [0,1,2,3]
and additionally:
另外:
[0,1,2,3].drop(2)
=> [2, 3]
[0,1,2,3].drop(3)
=> [3]
#4
116
[0, 132, 432, 342, 234][1..-1]
=> [132, 432, 342, 234]
So unlike shift
or slice
this returns the modified array (useful for one liners).
因此,与shift或slice不同的是,它返回了修改后的数组(对于一个内层来说是有用的)。
#5
87
This is pretty neat:
这是非常整洁:
head, *tail = [1, 2, 3, 4, 5]
#==> head = 1, tail = [2, 3, 4, 5]
As written in the comments, there's an advantage of not mutating the original list.
正如在评论中所写的,没有改变原始列表是有好处的。
#6
18
or a.delete_at 0
或a.delete_at 0
#7
11
Use shift method
使用转换方法
array.shift(n) => Remove first n elements from array
array.shift(1) => Remove first element
https://ruby-doc.org/core-2.2.0/Array.html#method-i-shift
https://ruby-doc.org/core-2.2.0/Array.html method-i-shift
#8
#9
4
You can use Array.delete_at(0) method which will delete first element.
您可以使用Array.delete_at(0)方法来删除第一个元素。
x = [2,3,4,11,0]
x.delete_at(0) unless x.empty? # [3,4,11,0]
#10
2
You can use:arr - [arr[0]]
or else arr - [arr.shift()]
or simply arr.shift(1)
您可以使用:arr - [arr[0]]或其他arr - [arr.shift()]或简单的arr.shift(1)
#11
0
You can use:
您可以使用:
a.delete(a[0])
a.delete_at 0
Both can work
都可以工作