ruby 集合 分组

In this program, we will see how we can combine the two sets? This is not a very difficult task. This can be easily done with the help of the + operator. In many places of programming, you will find that + operator is overloaded for various types to meet various purposes. Here in the case of sets, + operator works likes arithmetic OR. It combines all the elements from both the sets and returns a set that contains all those elements. Let us look at the code and understand how we can complete this task.

在此程序中,我们将看到如何将两个集合结合起来 ? 这不是一个非常困难的任务。 可以在+运算符的帮助下轻松完成此操作 。 在许多编程地方,您会发现+运算符对于各种类型都已重载以满足各种目的。 在集合的情况下, +运算符的作用类似于算术OR 。 它组合了两个集合中的所有元素,并返回包含所有这些元素的集合。 让我们看一下代码,了解如何完成此任务。

Methods used:

使用的方法:

  1. + : In ruby, most of the operators are considered as methods. This operator or method is used to combine two sets provided as arguments to the method. The return type of this operator is set itself. The set returned is having all the elements which are present in both the sets. Syntax:

    + :在Ruby中,大多数运算符被视为方法。 此运算符或方法用于合并作为方法的参数提供的两个集合。 此运算符的返回类型是自己设置的。 返回的集合具有两个集合中都存在的所有元素。 句法:

        SetA + SetB
    
    
  2. 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.each:-set.each方法用于逐个打印集合中的元素。 它将为您提供前进方向的元素

Variables used:

使用的变量:

  • Vegetable : It is an instance of Set class. It is the first argument passed as the argument in + operator.

    Vegetable :它是Set类的一个实例。 它是在+运算符中作为参数传递的第一个参数。

  • 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 :它包含从+运算符或方法返回的集合。

Program:

程序:

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

Output

输出量

potato element from new set
tomato element from new set
brinjal element from new set
onion element from new set
peas element from new set
beetroot element from new set
chilli element from new set
cabbage element from new set
capsicum element from new set

Explanation:

说明:

In the above code, it is shown how one can combine all the elements 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 observe that there is no repetition of elements. It is having all the elements and each element is present only for one time. Elements are appearing single even if they are present in both sets.

在上面的代码中,显示了如何组合两个集合中的所有元素? 如上所示,我们定义了三个集合,两个集合用于执行处理,一个集合用于存储两个集合中的公共元素。 我们从set.each方法获得帮助,以打印新集中的所有元素。 结果,您可以观察到元素没有重复。 它具有所有元素,每个元素仅出现一次。 即使元素出现在两个集合中,它们也都显示为单个。

翻译自: https://www.includehelp.com/ruby/join-all-the-elements-of-two-sets-in-ruby.aspx

ruby 集合 分组

ruby 集合 分组_将Ruby中两个集合的所有元素结合在一起相关推荐

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

    ruby 集合 分组 Finding differences simply means that finding elements that are uncommon between two sets ...

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

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

  3. 快速得到两个集合的并集java_Java 程序计算两个集合的并集

    Java 程序计算两个集合的并集 在此示例中,我们将学习计算Java中两个集合的并集. 要理解此示例,您应该了解以下Java编程主题: 示例1:使用addAll()计算两个集合的并集 示例import ...

  4. 集合:求A、B两个集合的交集、并集和补集的代码(C语言)

    集合:求A.B两个集合的交集.并集和补集的代码(C语言) #include <stdio.h> #define N 100 main(){int a[N],b[N],c[N],d[N];i ...

  5. python中可以作为定界符_在 Python 中,字典和集合都是用一对 作为定界符,字典的每个元素有两部分组成,即 和 ,其中 不允许重复。_学小易找答案...

    [多选题]路由器A的G0/0/1接口配置信息如下,下列说法正确的有?(多选) acl number 3000 rule 5 deny 17 rule 10 deny 89 rule 15 deny 6 ...

  6. python中两个集合的运算并交补_三种方式实现 Python 中的集合的交、并、补运算...

    文章目录 三种方式实现 Python 中的集合的交.并.补运算 一 背景 集合这个概念在我们高中阶段就有所了解,毕业已多年,我们一起回顾一下几个集合相关的基本概念吧? 集合是指具有某种特定性质的具体的 ...

  7. ruby 读取文本_使用Ruby进行文本处理

    ruby 读取文本 在与Perl或Python相同的页面上,Ruby具有强大的功能,可以成为强大的文本处理语言. 本文简要地讨论了Ruby的文本数据处理能力,以及如何使用它来有效处理文本数据的不同格式 ...

  8. java List最大_在java中获取List集合中最大的日期时间操作

    取list集合中最大的日期, 可以用date max = collections.max(datelist);, 传入一个日期集合, 就可以获取, 工作中有这个需求, 就查找到这个, 代码如下 } e ...

  9. python列表去重函数_对python中两种列表元素去重函数性能的比较方法

    测试函数: 第一种:list的set函数 第二种:{}.fromkeys().keys() 测试代码: #!/usr/bin/python #-*- coding:utf-8 -*- import t ...

最新文章

  1. Python实现线程池
  2. torch.nn.BCELoss are unsafe to autocast
  3. UEFI+GPT安装Windows8和CentOS双系统
  4. pyqt5 点击开始执行_《快速掌握PyQt5》第一章 PyQt5的起点
  5. poj1942——组合数学
  6. 软件测试用例编写方法
  7. 谷歌金山词霸合作版升级后出现CBSText.dll故障
  8. 干货!博应用APP推广的三大步骤五大原则
  9. FreebuF黑客专访系列之吴翰清(刺):接下来几年,有两样东西必定会火
  10. 7-5 分段函数计算 (15 分)
  11. 【网络科普】宽带电缆 vs DSL vs 光纤
  12. matlab实时编辑器怎么用,Markdown 实时编辑器
  13. 异形图片自动排版之装箱算法
  14. linux下文件的total是啥,linux中,ls -l命令显示的total的含义。
  15. 叉乘 线性代数_线性代数4——向量3(叉积、外积、向量积)
  16. Windows 驱动程序禁用数字签名
  17. 三年级计算机画图工具教案,小学三年级信息技术教学设计《画图》
  18. Extract Method(提炼函数)
  19. 使用Python获取Google,Yahoo金融数据工具pandas_datareader
  20. 【Unity】OnTriggerStay2D无法触发问题,物体跟随鼠标等背包系统以及UI场景交互系统技术总结

热门文章

  1. 设置密码命名是什么linux,orapwd 工具建立密码文件遵守的命名方法
  2. php curl cookie,php中curl获取返回页面的cookie
  3. 如何设置mysql表中文乱码_php mysql表中文乱码问题如何解决
  4. tensorboard报错:ValueError Duplicate plugins for name projector 问题的出现及解决过程
  5. JAVA构造函数是不是封装_Java 封装与构造函数
  6. html 图片墙效果,基于html5实现的图片墙效果
  7. Latex 调整表格大小 表格过大 表格过小
  8. bash shell是如何识别特殊符号的
  9. 洛谷P1879 [USACO06NOV]玉米田Corn Fields【状压dp】
  10. node.js 爬虫入门总结