删除Ruby哈希中的键数组

时间:2021-08-24 21:16:59

How would I go about deleting an array of keys in a hash? For example, you can call:

如何删除散列中的键数组?例如,你可以打电话:

hash_array.delete(some_key)

But how can I do this:

但是我怎么做呢:

hash_array.delete([key1,key2,key3,...])

without looping through the array unnecessarily.

没有不必要地在数组中循环。

6 个解决方案

#1


14  

You can iterate over an array of keys and delete everyone of them:

您可以遍历一个键数组并删除它们中的每个:

[key1, key2, key3].each { |k| some_hash.delete k }

Can't remember any better solution.

我想不出更好的解决办法了。

#2


11  

You can try to use Hash#delete_if:

您可以尝试使用散列#delete_if:

delete_if deletes every key-value pair from hsh for which block evaluates to true.

delete_if从hsh中删除每个键值对,该块的计算结果为true。

array_hash.delete_if { |key, _| [key1, key2, key3].include? key }

UPDATE If you don't want to iterate over array of keys, you can use Set instead of Array (since Set uses Hash as storage include? is O(1)):

如果您不想对键数组进行迭代,那么可以使用Set而不是array(因为Set使用Hash作为存储include?O(1)):

require 'set'
keys = [key1,key2,key3].to_set
array_hash.delete_if { |key, _| keys.include? key }

#3


10  

This is exactly what you are looking for... You can do it like this without looping through the array unnecessarily.

这正是你想要的……您可以这样做,而不必不必要地对数组进行循环。

keys_to_delete = [key1, key2, key3]
hash_array.except!(*keys_to_delete)

The result is stored in hash_array

结果存储在hash_array中

#4


2  

Maybe it's worth to make a method

也许用一种方法是值得的。

class Hash
  def delete_by_keys *keys
    keys.each{|k| delete(k)}
  end
end

hash_array.delete_by_keys(key1,key2,..)

#5


1  

You can achieve what you intend to do with the "splat operator" (*).

您可以使用“splat操作符”(*)实现您的目标。

hash_array.delete *[key1,key2,key3,...]

This is the equivalent to passing every key as a separate argument.

这相当于将每个键作为单独的参数传递。

#6


0  

ActiveSupport (part of Rails) implements exactly this, as Hash#except and Hash#except!

ActiveSupport (Rails的一部分)就是这样实现的,比如Hash#except和Hash#except!

def except!(*keys)
    keys.each { |key| delete(key) }
    self
end

See http://apidock.com/rails/v4.0.2/Hash/except%21

参见http://apidock.com/rails/v4.0.2/Hash/except%21

#1


14  

You can iterate over an array of keys and delete everyone of them:

您可以遍历一个键数组并删除它们中的每个:

[key1, key2, key3].each { |k| some_hash.delete k }

Can't remember any better solution.

我想不出更好的解决办法了。

#2


11  

You can try to use Hash#delete_if:

您可以尝试使用散列#delete_if:

delete_if deletes every key-value pair from hsh for which block evaluates to true.

delete_if从hsh中删除每个键值对,该块的计算结果为true。

array_hash.delete_if { |key, _| [key1, key2, key3].include? key }

UPDATE If you don't want to iterate over array of keys, you can use Set instead of Array (since Set uses Hash as storage include? is O(1)):

如果您不想对键数组进行迭代,那么可以使用Set而不是array(因为Set使用Hash作为存储include?O(1)):

require 'set'
keys = [key1,key2,key3].to_set
array_hash.delete_if { |key, _| keys.include? key }

#3


10  

This is exactly what you are looking for... You can do it like this without looping through the array unnecessarily.

这正是你想要的……您可以这样做,而不必不必要地对数组进行循环。

keys_to_delete = [key1, key2, key3]
hash_array.except!(*keys_to_delete)

The result is stored in hash_array

结果存储在hash_array中

#4


2  

Maybe it's worth to make a method

也许用一种方法是值得的。

class Hash
  def delete_by_keys *keys
    keys.each{|k| delete(k)}
  end
end

hash_array.delete_by_keys(key1,key2,..)

#5


1  

You can achieve what you intend to do with the "splat operator" (*).

您可以使用“splat操作符”(*)实现您的目标。

hash_array.delete *[key1,key2,key3,...]

This is the equivalent to passing every key as a separate argument.

这相当于将每个键作为单独的参数传递。

#6


0  

ActiveSupport (part of Rails) implements exactly this, as Hash#except and Hash#except!

ActiveSupport (Rails的一部分)就是这样实现的,比如Hash#except和Hash#except!

def except!(*keys)
    keys.each { |key| delete(key) }
    self
end

See http://apidock.com/rails/v4.0.2/Hash/except%21

参见http://apidock.com/rails/v4.0.2/Hash/except%21