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 集合 分组

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

  1. ruby 集合 分组_在Ruby中打印集合的元素

    ruby 集合 分组 We have gone through the implementation of sets in Ruby. They are very similar to arrays. ...

  2. ruby 集合 分组_将Ruby中两个集合的所有元素结合在一起

    ruby 集合 分组 In this program, we will see how we can combine the two sets? This is not a very difficul ...

  3. python两个时间内的工作日_如何在Python中找到两个日期之间的星期一或任何其他工作日的数目?...

    这是高效的-即使在开始和结束之间有一万天的时间-而且仍然非常灵活(它在sum函数内最多迭代7次):def intervening_weekdays(start, end, inclusive=True ...

  4. python中用plot绘制两条直线_在Matplotlib中绘制两条直线之间角度的最佳方法

    您可以使用^{}绘制相应角度度量值的弧. 绘制角弧: 定义一个函数,该函数可以接受2matplotlib.lines.Line2D个对象,计算角度并返回一个matplotlib.patches.Arc ...

  5. 如何在python中找到两个日期时间对象之间的时差?

    本文翻译自:How do I find the time difference between two datetime objects in python? 如何分辨两个datetime对象之间的时 ...

  6. 牛客题霸 [ 在二叉树中找到两个节点的最近公共祖先] C++题解/答案

    牛客题霸 [ 在二叉树中找到两个节点的最近公共祖先] C++题解/答案 题目描述 给定一棵二叉树以及这棵树上的两个节点 o1 和 o2,请找到 o1 和 o2 的最近公共祖先节点. 题解: 我们想想最 ...

  7. 在二叉树中找到两个节点的最近公共祖先(C++)

    在二叉树中找到两个节点的最近公共祖先 描述   给定一棵二叉树(保证非空)以及这棵树上的两个节点对应的val值 o1 和 o2,请找到 o1 和 o2 的最近公共祖先节点. 数据范围:树上节点数满足1 ...

  8. java合并两个set_Java中合并两个集合的方法

    要在JAVA中合并两个集合,代码如下所示的− 示例import java.util.stream.*; import java.util.*; import java.io.*; public cla ...

  9. python怎么验证两个json是不是一样_比较Python中的两个JSON树

    我有这种JSON树的文件夹结构.有什么方法可以将它与同类的Json树进行比较以获得差异(文件丢失或不同的文件属性(日期,crc,..)),并将其作为具有不同/缺失文件名称的列表返回.比较Python中 ...

最新文章

  1. 编写配置文件不能出现帮助信息
  2. 转载:Linux查看设置系统时区
  3. iOS的那个漏洞并不只是影响苹果设备CVE-2017-6975 Google称影响了所有使用Broadcom Wi-Fi SoC的设备...
  4. bzoj2002Bounce 弹飞绵羊
  5. 记录一次与大神们的关于GAN应用于NLP的讨论
  6. Robot Framework 使用1-环境配置及简单网站兼容性测试(转)
  7. [XSY3381] 踢罐子(几何)
  8. 无线网卡共享Internet访问到电脑有线接口注意事项
  9. 数据结构树的基本操作_数据结构二叉树的基本操作~~~~
  10. python import numpy_python之numpy
  11. C基础——文本格式和二进制格式的区别
  12. python基础教程第三版-Python基础教程(第三版)(七)再谈抽象
  13. 一文简说北斗定位系统的前世今生
  14. web前端开发前景如何
  15. SetupFactory使用
  16. [PowerQuery]Excel 如何开启PowerQuery组件
  17. 刘夏真的简历中国科学院计算机所,刘夏_广西医科大学研究生导师信息
  18. Android模仿新浪微博(自定义ListView下拉刷新)
  19. 迅软信息签约安龙基因医药技术构建医药行业防护体
  20. 梁宁《产品思维》之1发刊词

热门文章

  1. 树莓派4B (aarch64) 安装PyTorch 1.8 的可行方案
  2. excel表格从某个标志计算机,让Excel也玩多标签 多个图表一个窗口 -电脑资料
  3. linux流式访问日志,流式实时日志分析系统的实现原理
  4. mysql 101_MySQL 调优/优化的 101 个建议!
  5. Shell 信号发送与捕捉
  6. GO 从零开始的语法学习二
  7. Java基础17:Java IO流总结
  8. MAVEN 私有仓库库迁移
  9. html中可以自定义属性,,,妈的竟然才知道..
  10. Objective-C学习笔记(一)–类