ruby 如何按值从数组中删除一个元素
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10034678/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I delete one element from an array by value
提问by Tamik Soziev
I have an array of elements in Ruby
我在 Ruby 中有一组元素
[2,4,6,3,8]
I need to remove elements with value 3for example
我需要与价值移除元素3,例如
How do I do that?
我怎么做?
回答by Tamik Soziev
I think I've figured it out:
我想我已经弄清楚了:
a = [3, 2, 4, 6, 3, 8]
a.delete(3)
#=> 3
a
#=> [2, 4, 6, 8]
回答by Abram
Borrowing from Travisin the comments, this is a better answer:
在评论中从Travis借用,这是一个更好的答案:
I personally like
[1, 2, 7, 4, 5] - [7]which results in=> [1, 2, 4, 5]fromirb
我个人喜欢
[1, 2, 7, 4, 5] - [7]哪个结果=> [1, 2, 4, 5]来自irb
I modified his answer seeing that 3 was the third element in his example array. This could lead to some confusion for those who don't realize that 3 is in position 2 in the array.
我修改了他的答案,看到 3 是他的示例数组中的第三个元素。对于那些没有意识到 3 在数组中的位置 2 的人来说,这可能会导致一些混乱。
回答by Steve
Another option:
另外一个选项:
a = [2,4,6,3,8]
a -= [3]
which results in
这导致
=> [2, 4, 6, 8]
回答by Scott
I'm not sure if anyone has stated this, but Array.delete() and -= valuewill delete every instance of the value passed to it within the Array. In order to delete the first instance of the particular element you could do something like
我不确定是否有人说过这一点,但 Array.delete() 和 -= value将删除数组中传递给它的值的每个实例。为了删除特定元素的第一个实例,您可以执行以下操作
arr = [1,3,2,44,5]
arr.delete_at(arr.index(44))
#=> [1,3,2,5]
There could be a simpler way. I'm not saying this is best practice, but it is something that should be recognized.
可能有更简单的方法。我并不是说这是最佳实践,但这是应该承认的。
回答by Sayan
Assuming you want to delete 3 by value at multiple places in an array, I think the ruby way to do this task would be to use the delete_if method:
假设您想在数组中的多个位置按值删除 3,我认为执行此任务的 ruby 方法是使用 delete_if 方法:
[2,4,6,3,8,3].delete_if {|x| x == 3 }
You can also use delete_if in removing elements in the scenario of 'array of arrays'.
您还可以在“数组数组”场景中使用 delete_if 删除元素。
Hope this resolves your query
希望这能解决您的疑问
回答by barlop
I like the -=[4]way mentioned in other answers to delete the elements whose value are 4.
我喜欢-=[4]其他答案中提到的删除值为 4 的元素的方式。
But there is this way:
但是有这种方式:
irb(main):419:0> [2,4,6,3,8,6].delete_if{|i|i==6}
=> [2, 4, 3, 8]
irb(main):420:0>
mentioned somewhere in "Basic Array Operations", after it mentions the mapfunction.
在“基本数组操作”中的某处提到了该map函数之后。
回答by rilutham
You can simply run:
你可以简单地运行:
[2,4,6,3,8].delete(3)
回答by Sawo Cliff
A .delete_at(3)3here being the position.
A.delete_at(3)3这里是位置。
回答by the Tin Man
Here are some benchmarks:
以下是一些基准:
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
And again with a bigger array containing lots of duplicates:
再次使用包含大量重复项的更大数组:
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
And even bigger with more duplicates:
甚至更大,更多重复:
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
回答by Rodrigo Mendon?a
I improved Niels's solution
我改进了 Niels 的解决方案
class Array
def except(*values)
self - values
end
end
Now you can use
现在你可以使用
[1, 2, 3, 4].except(3, 4) # return [1, 2]
[1, 2, 3, 4].except(4) # return [1, 2, 3]

