从数组 Ruby 中删除元素
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8929053/
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
Removing elements from array Ruby
提问by Michael
Let's say I am trying to remove elements from array a = [1,1,1,2,2,3]. If I perform the following:
假设我正在尝试从 array 中删除元素a = [1,1,1,2,2,3]。如果我执行以下操作:
b = a - [1,3]
Then I will get:
然后我会得到:
b = [2,2]
However, I want the result to be
但是,我希望结果是
b = [1,1,2,2]
i.e. I only remove one instance of each element in the subtracted vector not all cases. Is there a simple way in Ruby to do this?
即我只删除减去向量中每个元素的一个实例,而不是所有情况。在 Ruby 中有一种简单的方法可以做到这一点吗?
回答by Norm212
You may do:
你可以这样做:
a= [1,1,1,2,2,3]
delete_list = [1,3]
delete_list.each do |del|
a.delete_at(a.index(del))
end
result : [1, 1, 2, 2]
结果:[1, 1, 2, 2]
回答by zetetic
[1,3].inject([1,1,1,2,2,3]) do |memo,element|
memo.tap do |memo|
i = memo.find_index(e)
memo.delete_at(i) if i
end
end
回答by seph
Not very simple but:
不是很简单,但是:
a = [1,1,1,2,2,3]
b = a.group_by {|n| n}.each {|k,v| v.pop [1,3].count(k)}.values.flatten
=> [1, 1, 2, 2]
Also handles the case for multiples in the 'subtrahend':
还处理“减数”中倍数的情况:
a = [1,1,1,2,2,3]
b = a.group_by {|n| n}.each {|k,v| v.pop [1,1,3].count(k)}.values.flatten
=> [1, 2, 2]
EDIT: this is more an enhancement combining Norm212 and my answer to make a "functional" solution.
编辑:这更像是将 Norm212 和我的答案结合起来制作“功能性”解决方案的增强功能。
b = [1,1,3].each.with_object( a ) { |del| a.delete_at( a.index( del ) ) }
Put it in a lambda if needed:
如果需要,将其放入 lambda 中:
subtract = lambda do |minuend, subtrahend|
subtrahend.each.with_object( minuend ) { |del| minuend.delete_at( minuend.index( del ) ) }
end
then:
然后:
subtract.call a, [1,1,3]
回答by Lyres
a = [1,1,1,2,2,3]
a.slice!(0) # remove first index
a.slice!(-1) # remove last index
# a = [1,1,2,2] as desired
回答by The Whiz of Oz
A simple solution I frequently use:
我经常使用的一个简单的解决方案:
arr = ['remove me',3,4,2,45]
arr[1..-1]
=> [3,4,2,45]
回答by Cary Swoveland
For speed, I would do the following, which requires only one pass through each of the two arrays. This method preserves order. I will first present code that does not mutate the original array, then show how it can be easily modified to mutate.
为了速度,我将执行以下操作,只需要通过两个数组中的每一个。此方法保留顺序。我将首先展示不会改变原始数组的代码,然后展示如何轻松修改它以进行变异。
arr = [1,1,1,2,2,3,1]
removals = [1,3,1]
h = removals.group_by(&:itself).transform_values(&:size)
#=> {1=>2, 3=>1}
arr.each_with_object([]) { |n,a|
h.key?(n) && h[n] > 0 ? (h[n] -= 1) : a << n }
#=> [1, 2, 2, 1]
arr
#=> [1, 1, 1, 2, 2, 3, 1]
To mutate arrwrite:
变异arr写:
h = removals.group_by(&:itself).transform_values(&:count)
arr.replace(arr.each_with_object([]) { |n,a|
h.key?(n) && h[n] > 0 ? (h[n] -= 1) : a << n })
#=> [1, 2, 2, 1]
arr
#=> [1, 2, 2, 1]
This uses the 21stcentury method Hash#transform_values(new in MRI v2.4), but one could instead write:
它使用了21日世纪的方法散列#transform_values(MRI中新V2.4),而是一个可能代替写:
h = Hash[removals.group_by(&:itself).map { |k,v| [k,v.size] }]
or
或者
h = removals.each_with_object(Hash.new(0)) { | n,h| h[n] += 1 }

