ruby 集合 分组_在Ruby中找到两个集合之间的区别

ruby 集合 分组

Finding differences simply means that finding elements that are uncommon between two sets as well as are only present in the first set. We can find this, with the help of a – operator. You can also consider the objective as to find the unique elements from the first set or you can say that the set which is passed as the first argument.

发现差异只是意味着找到在两组之间不常见且仅在第一组中存在的元素 。 我们可以在–运算符的帮助下找到它。 您也可以考虑从第一个集合中找到唯一元素的目的,或者可以说该集合作为第一个参数传递。

Methods used:

使用的方法:

  • -: In ruby, most of the operators are considered as methods. This operator or method is used to find out the unique elements from the set which is provided as the first argument to the - method. The return type of this operator is a set itself.

    - :在Ruby中,大多数运算符被视为方法。 此运算符或方法用于从集合中查找唯一元素,该元素作为-方法的第一个参数提供。 此运算符的返回类型本身就是集合。

  • Syntax: 句法:
    SetA - SetB

  • set.each: set.each method is used to print the elements from the set one by one. It will provide you elements in the forward direction.

    set.eachset.each方法用于逐个打印集合中的元素。 它将为您提供前进方向的元素。

  • Variables used:

    使用的变量:

    • Vegetable: It is a set. It is the first argument passed as the argument in & operator.

      蔬菜 :这是一组。 它是在&运算符中作为参数传递的第一个参数。

    • Sabzi: It is an instance of Set class. It is the second argument that is passed in & operator.

      Sabzi :它是Set类的实例。 这是在&运算符中传递的第二个参数。

    • New_set: It is containing the set which is returned from the & operator or method.

      New_set :它包含从&运算符或方法返回的集合。

    Code:

    码:

    =begin
    Ruby program to show implementation of - operator
    =end
    
    require 'set'
    
    Vegetable=Set.new(["potato","broccolli","broccoflower","lentils","peas","fennel","chilli","cabbage"])
    
    Sabzi=Set.new(["potato","tomato","brinjal","onion","beetroot","capsisum","chilli"])
    
    New_set = Vegetable - Sabzi
    
    New_set.each do |string|
    	puts "#{string} element from new set"
    end
    
    

    Output

    输出量

    broccoli element from new set
    broccoflower element from new set
    lentils element from new set
    peas element from new set
    fennel element from new set
    cabbage element from new set
    
    

    Explanation:

    说明:

    In the above code, it is shown how one can find the unique elements from the first set by finding the difference from both the sets. As you can see above, we have defined three sets, two sets are for carrying out the processing and one set is for storing the common elements from both the sets. We have taken help from the set.each method to print all the elements from the new set. As a result, you can find that the new set contains all the elements which are not present in the second set. This method would not give you elements that are unique in the second set.

    在上面的代码中,显示了如何通过查找两个集合之间的差异来从第一个集合中找到唯一元素 。 如上所示,我们定义了三个集合,两个集合用于执行处理,一个集合用于存储两个集合中的公共元素。 我们从set.each方法获得帮助,以打印新集中的所有元素。 结果,您会发现新集合包含第二个集合中不存在的所有元素。 此方法不会为您提供第二组中唯一的元素。

    翻译自: https://www.includehelp.com/ruby/find-the-difference-between-two-sets.aspx

    ruby 集合 分组

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值