简体   繁体   English

在 Ruby 中,array.select { |x| array.count(x) > 1 } 的意思是什么?

[英]In Ruby what does array.select { |x| array.count(x) > 1 } mean in words?

What does array.count(x) mean in the following: array.count(x)的含义如下:

arr1 = [1, 1, 3, 2, 6, 7, 9 ,6, 5, 1, 8, 9, 3]

def dups (array)
  duplicates = array.select { |x| array.count(x) > 1} #the (x) 
  duplicates.uniq
end

p dups(arr1) => [1,3,6,9]

The result is what I want, but I'm having a hard time understanding when I say what the block of code is doing.结果是我想要的,但是当我说代码块在做什么时,我很难理解。 I've been playing around with the code and removing (x) to see that I then just get all the unique numbers in the array once, but I can't get through the array.count(x) > 1 part.我一直在玩代码并删除(x)以查看我只获取数组中的所有唯一数字一次,但我无法通过array.count(x) > 1部分。

select is a filtering method. select是一种过滤方法。 It passes each element to the block, and the block determines whether that element should be included in the output by returning true or false .它将每个元素传递给块,块通过返回truefalse来确定该元素是否应包含在 output 中。

The block in your code does so by counting how often that element occurs in the array.代码中的块通过计算该元素在数组中出现的频率来实现。 If it occurs more than once, the block returns true , and select includes the corresponding element in the output.如果多次出现,则该块返回true ,并且select包含 output 中的相应元素。

It might help to put x , x 's count, and the block's return value in a table:xx的计数和块的返回值放在表中可能会有所帮助:

 x | count(x) | count(x) > 1
---+----------+-------------
 1 | 3        | true
 1 | 3        | true
 3 | 2        | true
 2 | 1        | false
 6 | 2        | true
 7 | 1        | false
 9 | 2        | true
 6 | 2        | true
 5 | 1        | false
 1 | 3        | true
 8 | 1        | false
 9 | 2        | true
 3 | 2        | true

Your select returns all elements with a count above 1: (those with true in the last column)您的select返回count大于 1 的所有元素:(最后一列中为true的元素)

array = [1, 1, 3, 2, 6, 7, 9, 6, 5, 1, 8, 9, 3]
array.select { |x| array.count(x) > 1 }
#=> [1, 1, 3, 6, 9, 6, 1, 9, 3]

uniq then gets rid of the duplicates: uniq然后摆脱重复:

array.select { |x| array.count(x) > 1 }.uniq
#=> [1, 3, 6, 9]

You might have noticed that count has to traverse array once for each element in order to count its occurrences.您可能已经注意到count必须为每个元素遍历array一次才能计算其出现次数。

It's much faster to use a hash for counting the elements:使用 hash 来计算元素要快得多:

counts = Hash.new(0)
array.each { |x| hash[x] += 1 }
counts
#=> {1=>3, 3=>2, 2=>1, 6=>2, 7=>1, 9=>2, 5=>1, 8=>1}

Counting occurrences is so common that Ruby 2.7 has a dedicated method tally :计数出现如此普遍,以至于 Ruby 2.7 有一个专用的方法tally

counts = array.tally
#=> {1=>3, 3=>2, 2=>1, 6=>2, 7=>1, 9=>2, 5=>1, 8=>1}

Now we can simple select the keys with a value above 1:现在我们可以简单的 select 值大于 1 的键:

counts.select { |k, v| v > 1 }
#=> {1=>3, 3=>2, 6=>2, 9=>2}

counts.select { |k, v| v > 1 }.keys
#=> [1, 3, 6, 9]

TL;DR TL;博士

The code is using Array#select to return elements that have more than one match within your Array object , based on an Array#count greater than 1. A breakdown of how #select and #count interact to make this happen is outlined below.该代码使用Array#select根据大于 1 的Array#count返回在Array object中具有多个匹配项的元素。下面概述了 #select 和 #count 如何交互以实现此目的的细目。

Analysis and Explanation分析说明

array.select { |x| array.count(x) > 1}

Array#select is important here, because it's saying: Array#select在这里很重要,因为它说:

For each element of array , return that element if the following block evaluates as truthy.对于array的每个元素,如果以下块评估为真,则返回该元素。

When the element passed to the block has more than one match within the Array, that element is returned.当传递给块的元素在 Array 中有多个匹配项时,将返回该元素。 If there's only one match within the Array (zero matches doesn't make sense with your example) then that element is not returned.如果数组中只有一个匹配项(零匹配项对您的示例没有意义),则不会返回该元素。

In plain language, the block means:用简单的语言来说,块的意思是:

For each element of array temporarily stored in scoped variable x , use the Array#count method to return the number of matching elements in the array that are greater than 1 .对于临时存储在作用域变量x中的数组的每个元素,使用Array#count方法返回数组中大于1的匹配元素的数量。 The code assumes that the variable stored in x can be compared against an Integer (in this case, the value 1 ).该代码假定存储在x中的变量可以与 Integer (在本例中为值1 )进行比较。

In short, #select maps elements onto the block, the block evaluates the number of matches using #count, and then the elements that have multiple matches are returned as a new Array object.简而言之,#select 将元素映射到块上,块使用 #count 计算匹配数,然后将具有多个匹配的元素作为新数组 object 返回。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM