我怎样才能从数组中删除一个元素的值

我有一个Ruby中的元素数组

[2,4,6,3,8] 

例如,我需要删除值为3元素

我怎么做?

我想我已经明白了:

 a = [2,4,6,3,8] a.delete(3) 

从评论中借用特拉维斯 ,这是一个更好的答案:

我个人喜欢[1, 2, 7, 4, 5] - [7] ,从irb导致=> [1, 2, 4, 5] irb => [1, 2, 4, 5]

我修改了他的答案,看到3是他的例子数组中的第三个元素。 对于那些没有意识到3在arrays中位置2的人,这可能会导致一些混淆。

另外一个select:

 a = [2,4,6,3,8] a -= [3] 

这导致了

 => [2, 4, 6, 8] 

我喜欢其他答案中提到的-=[4]方法来删除其值为4的元素。

但是有这样的方式:

 irb(main):419:0> [2,4,6,3,8,6].delete_if{|i|i==6} => [2, 4, 3, 8] irb(main):420:0> 

在提到mapfunction之后,在“ 基本数组操作 ”的某个地方提到。

A .delete_at(3) 3这里是位置。

你可以简单地运行:

 [2,4,6,3,8].delete(3) 

假设你想在数组中的多个地方删除值3,我认为ruby做这个任务的方法是使用delete_if方法:

 [2,4,6,3,8,3].delete_if {|x| x == 3 } 

您还可以在“数组数组”的场景中使用delete_if来移除元素。

希望这可以解决您的查询

我不确定是否有人说过,但是Array.delete()和 – = value会删除在数组中传递给它的每个值。 为了删除特定元素的第一个实例,你可以做类似的事情

 arr = [1,3,2,44,5] arr.delete_at(arr.index(44)) #=> [1,3,2,5] 

可以有一个更简单的方法。 我不是说这是最好的做法,但这是应该承认的。

这里有一些基准:

 require 'fruity' class Array def rodrigo_except(*values) self - values end def niels_except value value = value.kind_of?(Array) ? value : [value] self - value end end ARY = [2,4,6,3,8] compare do soziev { a = ARY.dup; a.delete(3); a } steve { a = ARY.dup; a -= [3]; a } barlop { a = ARY.dup; a.delete_if{ |i| i == 3 }; a } rodrigo { a = ARY.dup; a.rodrigo_except(3); } niels { a = ARY.dup; a.niels_except(3); } end # >> Running each test 4096 times. Test will take about 2 seconds. # >> soziev is similar to barlop # >> barlop is faster than steve by 2x ± 1.0 # >> steve is faster than rodrigo by 4x ± 1.0 # >> rodrigo is similar to niels 

再次包含大量重复的数组:

 class Array def rodrigo_except(*values) self - values end def niels_except value value = value.kind_of?(Array) ? value : [value] self - value end end ARY = [2,4,6,3,8] * 1000 compare do soziev { a = ARY.dup; a.delete(3); a } steve { a = ARY.dup; a -= [3]; a } barlop { a = ARY.dup; a.delete_if{ |i| i == 3 }; a } rodrigo { a = ARY.dup; a.rodrigo_except(3); } niels { a = ARY.dup; a.niels_except(3); } end # >> Running each test 16 times. Test will take about 1 second. # >> steve is faster than soziev by 30.000000000000004% ± 10.0% # >> soziev is faster than barlop by 50.0% ± 10.0% # >> barlop is faster than rodrigo by 3x ± 0.1 # >> rodrigo is similar to niels 

甚至更大的重复:

 class Array def rodrigo_except(*values) self - values end def niels_except value value = value.kind_of?(Array) ? value : [value] self - value end end ARY = [2,4,6,3,8] * 100_000 compare do soziev { a = ARY.dup; a.delete(3); a } steve { a = ARY.dup; a -= [3]; a } barlop { a = ARY.dup; a.delete_if{ |i| i == 3 }; a } rodrigo { a = ARY.dup; a.rodrigo_except(3); } niels { a = ARY.dup; a.niels_except(3); } end # >> Running each test once. Test will take about 6 seconds. # >> steve is similar to soziev # >> soziev is faster than barlop by 2x ± 0.1 # >> barlop is faster than niels by 3x ± 1.0 # >> niels is similar to rodrigo 

我改进了Niels的解决scheme

 class Array def except(*values) self - values end end 

现在你可以使用

 [1, 2, 3, 4].except(3, 4) # return [1, 2] [1, 2, 3, 4].except(4) # return [1, 2, 3] 

你也可以猴子补丁。 我从来没有理解为什么Ruby有一个except Hash方法,但不是Array

 class Array def except value value = value.kind_of(Array) ? value : [value] self - value end end 

现在你可以做:

 [1,3,7,"436",354,nil].except(354) #=> [1,3,7,"436",nil] 

要么:

 [1,3,7,"436",354,nil].except([354, 1]) #=> [3,7,"436",nil] 

所以当你有多次出现的3,而你只想删除第一次出现的3时,你可以简单地做一些事情如下。

 arr = [2, 4, 6, 3, 8, 10, 3, 12] arr.delete_at arr.index 3 #This will modify arr as [2, 4, 6, 8, 10, 3, 12] where first occurrence of 3 is deleted. Returns the element deleted. In this case => 3.