如何选择独特的元素 [英] How to select unique elements

查看:130
本文介绍了如何选择独特的元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用 uniq_elements它会返回一个多重的元素方法延长阵列类。我也想用瓶盖到我的新方法与 uniq的。例如:

I would like to extend the Array class with a uniq_elements method which returns those elements with multiplicity of one. I also would like to use closures to my new method as with uniq. For example:

t=[1,2,2,3,4,4,5,6,7,7,8,9,9,9]
t.uniq_elements # => [1,3,5,6,8]

与闭合例子:

t=[1.0, 1.1, 2.0, 3.0, 3.4, 4.0, 4.2, 5.1, 5.7, 6.1, 6.2]
t.uniq_elements{|z| z.round} # => [2.0, 5.1]

无论是 T-t.uniq 也不 t.to_set-t.uniq.to_set 的作品。我不在乎速度,我在程序中调用它只有一次,所以它可能是一个缓慢的。

Neither t-t.uniq nor t.to_set-t.uniq.to_set works. I don't care of speed, I call it only once in my program, so it can be a slow.

推荐答案

Helper方法

此方法使用助手:

class Array
  def difference(other)
    dup.tap do |cpy|
      other.each do |e|
        ndx = cpy.index(e)
        cpy.delete_at(ndx) if ndx
      end
    end
  end
end

此方法类似于阵列# - 。所不同的是在下面的例子中示出:

This method is similar to Array#-. The difference is illustrated in the following example:

a = [3,1,2,3,4,3,2,2,4]
b = [2,3,4,4,3,4]

a - b              #=> [1]
c = a.difference b #=> [1, 3, 2, 2] 

正如你所见, A 包含三个3的和 B 包含两个,这样的第一个 2个3在 A 在构建 C A 不突变)。当 B 包含的至少多达做一个元素的实例 A C 中没有包含任何元素的实例。要删除在年底开始元素 A

As you see, a contains three 3's and b contains two, so the first two 3's in a are removed in constructing c (a is not mutated). When b contains as least as many instances of an element as does a, c contains no instances of that element. To remove elements beginning at the end of a:

a.reverse.difference(b).reverse #=> [3, 1, 2, 2]

阵列#区别!可在明显的方式来定义。

Array#difference! could be defined in the obvious way.

我已发现很多用途此方法(例如,<一href=\"http://stackoverflow.com/questions/29584327/find-a-unique-element-in-a-compound-array/29585396#29585396\">here, <一href=\"http://stackoverflow.com/questions/29966975/how-to-sort-an-array-by-amount-of-unique-groups-it-can-create/29969924#29969924\">here, <一href=\"http://stackoverflow.com/questions/29886736/how-to-check-if-two-string-contains-the-same-character-in-ruby/29887358#29887358\">here, <一href=\"http://stackoverflow.com/questions/28650041/get-an-array-of-arrays-with-unique-elements?nah=1#28820565\">here, <一href=\"http://stackoverflow.com/questions/30611939/find-a-duplicate-in-an-array-ruby/30612075#30611939\">here, <一href=\"http://stackoverflow.com/questions/30831946/matching-exact-elements-in-an-array-in-ruby/30832762#30832762\">here, <一href=\"http://stackoverflow.com/questions/31104241/ruby-koan-182-greed-dice/31107937#comment50256692_31107937\">here, <一href=\"http://stackoverflow.com/questions/31245594/excluding-first-element-by-value-from-array\">here, <一href=\"http://stackoverflow.com/questions/30121533/method-for-splitting-array-into-element-remaining-elements-pairs/31896663#31896663\">here, <一href=\"http://stackoverflow.com/questions/32035782/function-that-returns-the-number-of-letters-that-repeat-in-a-string/32038321#32038321\">here, <一href=\"http://stackoverflow.com/questions/32864181/how-can-i-find-the-first-non-recurring-letter-of-a-string-in-ruby/32871760#32871760\">here, <一href=\"http://stackoverflow.com/questions/32729134/cant-get-ruby-method-to-pass-spec-for-anagram-method/32919483#32919483\">here, <一href=\"http://stackoverflow.com/questions/33299204/combinaison-ruby-array-multidimensionnel-to-get-a-array-two-dimensional/33305906#33305906\">here, <一href=\"http://stackoverflow.com/questions/33793393/return-similar-elements-of-array-in-ruby/33793640#33793640\">here, <一href=\"http://stackoverflow.com/questions/36635399/ruby-remove-one-element-from-array/36637279#36637279\">here, <一href=\"http://stackoverflow.com/questions/36682667/how-do-i-compare-integer-indexes-in-arrays-when-there-are-duplicate-values/36686250#36686250\">here和<一个href=\"http://stackoverflow.com/questions/36211679/most-efficient-way-to-count-duplicated-elements-between-two-arrays#comment61463467_36211679\">here).

I have found many uses for this method (for example, here, here, here, here, here, here, here, here, here, here, here, here, here, here, here, here and here).

我有提议的,该方法被添加到红宝石芯

I have proposed that this method be added to the Ruby core.

在与阵列#使用 - ,这个方法可以很容易地提取数组的独特元素 A

When used with Array#-, this method makes it easy to extract the unique elements from an array a:

a = [1,3,2,4,3,4]
u = a.uniq          #=> [1, 2, 3, 4]
u - a.difference(u) #=> [1, 2]

这工作,因为

a.difference(u)     #=> [3,4]    

包含 A 的所有非唯一元素(每个可能不止一次)。

contains all the non-unique elements of a (each possibly more than once).

问题在手

code

class Array
  def uniq_elements(&prc)
    prc ||= ->(e) { e }
    a = map { |e| prc[e] }
    u = a.uniq
    uniques = u - a.difference(u)
    select { |e| uniques.include?(prc[e]) ? (uniques.delete(e); true) : false }
  end
end

例子

t = [1,2,2,3,4,4,5,6,7,7,8,9,9,9]
t.uniq_elements
  #=> [1,3,5,6,8]

t = [1.0, 1.1, 2.0, 3.0, 3.4, 4.0, 4.2, 5.1, 5.7, 6.1, 6.2]
t.uniq_elements { |z| z.round }
  # => [2.0, 5.1]

这篇关于如何选择独特的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆