如何从哈希中删除nil键?

时间:2022-08-18 07:27:25

With Ruby 2.4, I want to count the number of occurrences of an element and store them in a hash. However, I don't want to include any nil keys in my hash. So I tried

使用Ruby 2.4,我希望计算元素出现的次数,并将它们存储在散列中。但是,我不想在哈希中包含任何nil键。所以我试着

my_hash = int_data_col.each_with_object(Hash.new(0)) { |i, h| h[i]+=1 }.delete(nil)

but this returns "1". If I leave off the "delete(nil)", it returns a hash, but then a nil key is included in the hash (assuming a nil was present in the "int_data_col" array). How do I remove the nil key from my hash and still get the correct results?

但这返回“1”。如果我删掉"delete(nil)",它返回一个散列,但是在散列中包含一个nil键(假设在“int_data_col”数组中存在一个nil)。如何从哈希中删除nil键并仍然得到正确的结果?

3 个解决方案

#1


2  

Use Array#compact which removes all nil values before the count.

使用数组#compact,它在计数之前删除所有nil值。

my_hash = int_data_col.compact.each_with_object(Hash.new(0)) { |i, h| h[i]+=1 }

#2


2  

In Ruby 2.4, there's a way to do it which is very readable:

在Ruby 2.4中,有一种方法非常容易读懂:

arr.compact.group_by(&:itself).transform_values(&:size)

#3


0  

If you want to clean up the array prior to hashing it, then

如果您想在对数组进行散列之前清理它,那么

int_data_col.compact! 
int_data_col.each_with_object(Hash.new(0)) {|i, h| h[i]+=1 }

If you want to simply create the hash, ignoring nil, without disturbing the original array, then the following as suggested by @sagarpandya82

如果您只想创建散列,忽略nil,而不干扰原始数组,则按照@sagarpandya82的建议进行如下操作

int_data_col.compact.each_with_object(Hash.new(0)) { |i, h| h[i]+=1 }

When you had specified .delete(nil), I think you were expecting it to return the hash with the nil key removed. But what it did was remove the nil key and returned the count value of the nil key (1). From the Hash Class doc, "Deletes the key-value pair and returns the value from hash whose key is equal to key."

当您指定了。delete(nil)时,我认为您希望它返回一个散列,并删除nil键。但是它所做的是删除nil键并返回nil键(1)的计数值。

#1


2  

Use Array#compact which removes all nil values before the count.

使用数组#compact,它在计数之前删除所有nil值。

my_hash = int_data_col.compact.each_with_object(Hash.new(0)) { |i, h| h[i]+=1 }

#2


2  

In Ruby 2.4, there's a way to do it which is very readable:

在Ruby 2.4中,有一种方法非常容易读懂:

arr.compact.group_by(&:itself).transform_values(&:size)

#3


0  

If you want to clean up the array prior to hashing it, then

如果您想在对数组进行散列之前清理它,那么

int_data_col.compact! 
int_data_col.each_with_object(Hash.new(0)) {|i, h| h[i]+=1 }

If you want to simply create the hash, ignoring nil, without disturbing the original array, then the following as suggested by @sagarpandya82

如果您只想创建散列,忽略nil,而不干扰原始数组,则按照@sagarpandya82的建议进行如下操作

int_data_col.compact.each_with_object(Hash.new(0)) { |i, h| h[i]+=1 }

When you had specified .delete(nil), I think you were expecting it to return the hash with the nil key removed. But what it did was remove the nil key and returned the count value of the nil key (1). From the Hash Class doc, "Deletes the key-value pair and returns the value from hash whose key is equal to key."

当您指定了。delete(nil)时,我认为您希望它返回一个散列,并删除nil键。但是它所做的是删除nil键并返回nil键(1)的计数值。