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

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/541301.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/541301.shtml
英文地址,請注明出處:http://en.pswp.cn/news/541301.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

怎樣在linux卸載java,卸載linux自帶java,linux自帶java

卸載linux自帶java,linux自帶java第一步:rpm查詢java安裝包名稱[rootlocalhost java]# rpm -qa | grep javajava-1.7.0-openjdk-headless-1.7.0.51-2.4.5.5.el7.x86_64tzdata-java-2014b-1.el7.noarchpython-javapackages-3.4.1-5.el7.noarchjava-1.7.0-…

Swift iOS : 內存管理

Swift是自動管理內存的。這意味著,你不需要主動釋放內存。 比如Foo內包含的Bar,可以隨同Foo一起被釋放: import UIKit UIApplicationMain class AppDelegate: UIResponder, UIApplicationDelegate {var window : UIWindow?func application(…

python實現接口_Python | 使用類實現接口

python實現接口In this program, we are implementing the concept of Interface using class. Here, Class Shape worked as Interface. In Interface all methods must be non-implemented it must be implemented in child class unlike abstract class, where we can have …

linux lp 打印中文,Linux基礎命令---lp打印文件

lplp指令用來打印文件,也可以修改存在的打印任務。使用該指令可以指定打印的頁碼、副本等。此命令的適用范圍:RedHat、RHEL、Ubuntu、CentOS、Fedora、openSUSE、SUSE。1、語法lp [ -E ] [ -U username ] [ -c ] [ -d destination[/instance] ] [ -h…

【轉載】瀏覽器緩存詳解:expires cache-control last-modified

下面的內容展示了一個常見的 Response Headers,這些 Headers 要求客戶端最多緩存 3600 秒,也給出了一個 pub1259380237;gz 的校驗值。 HTTP/1.x 200 OK Transfer-Encoding: chunked Date: Sat, 28 Nov 2009 04:36:25 GMT Server: LiteSpeed Connection: …

ctype函數_PHP ctype_xdigit()函數與示例

ctype函數PHP ctype_xdigit()函數 (PHP ctype_xdigit() function) ctype_xdigit() function is a character type (CType) function in PHP, it is used to check whether a given string contains hexadecimal digits or not. ctype_xdigit()函數是PHP中的字符類型(CType)函數…

linux ldd運行不成功,Linux_Linux:Ldd命令介紹及使用方法,1、首先ldd不是一個可執行程序 - phpStudy...

Linux:Ldd命令介紹及使用方法1、首先ldd不是一個可執行程序,而只是一個shell腳本2、ldd能夠顯示可執行模塊的dependency,其原理是通過設置一系列的環境變量,如下:LD_TRACE_LOADED_OBJECTS、LD_WARN、LD_BIND_NOW、LD_L…

開發原生的 Google 眼鏡應用 【已翻譯100%】(2/2)

使用傳感器 Glass沒有鍵盤或觸摸屏,但仍然具有移動設備所有的標準的傳感器。你可以使用標準的傳感器組件來訪問這些傳感器。 定位和GPS Glass內置有GPS。TLocationSensor具有一個OnLocationChanged事件,這一事件在GPS組件被激活時產生,在有除…

linux下php的安裝,Linux下PHP安裝

1 下載php源碼安裝包 個人是php-5.6.30.tar.gzphp2 解壓文件mysqltar -zxvf php-5.6.30.tar.gznginx3 編譯安裝sqlcd php-5.6.30api建立www用戶和www用戶組curlgroupadd wwwsocketuseradd -g www wwwphp-fpm在編譯以前先把依賴包都裝上urlyum install curl curl-develyum inst…

PHP array_filter()函數與示例

PHP array_filter()函數 (PHP array_filter() Function) array_filter() function is used to apply a filter on array elements based on the function and returns the array with filtered elements, it accepts an array to be checked and a callback function. The call…

微軟宣布以 262 億美元現金收購 LinkedIn

北京時間6月13日20:45(美國當地時間6月13日上午8:45),微軟(Nasdaq: MSFT)和LinkedIn(領英,NYSE: LNKD)對外宣布,雙方達成協議,微軟宣布將以每股196美元、總價262億美元的價格收購LinkedIn,由現金…

linux vim 快速定位位置,vim快速移動定位的一些操作命令

很多時候你在編輯文件時移動光標都會花很長時間,這樣就無法高效的編輯了,要想達到高效編輯最好能夠將光標馬上定位到你需要的位置,vim就是可以滿足你這樣需求的一款編輯器,本節就要講一下vim快速移動定位的一些操作命令。下面的操…

本月初 本月末 java_本月內容作家(2018年8月)

本月初 本月末 javaWe are feeling glad to announce that Prerana Jain is the Content Writer Of The Month (August 2018) on the basis of her excellent contribution and her support to us. 我們很高興地宣布, Prera??na Jain憑借其出色的貢獻和對我們的支…

“玲瓏杯”線上賽 Round #15 河南專場 F 咸魚文章

elttiL moT nwod eht teerts sllac ruo god ” ehT peek god ” . piZ si a peehs god . tuB nehw moT seirt ot yas ” peeS ” , ti semoc tuo ” peek ” . dnA ni a yaw moT si thgir . piZ si syawla gnignirb sgniht oh rof su ot peek ! ll’I llet uoy tuoba emos fo m…

線性代數向量內積_向量的外積| 使用Python的線性代數

線性代數向量內積Prerequisites: 先決條件: Defining Vector using Numpy 使用Numpy定義向量 Learn: how to code for outer product of two vectors using am inbuilt function in numpy library? 了解:如何使用numpy庫中的內置函數為兩個向量的外部乘…

c語言案例朗讀工具源碼,C語言編寫簡單朗讀小工具(有源碼)

原標題:C語言編寫簡單朗讀小工具(有源碼)最近不少人在后臺留言說學C都是面對枯燥的控制臺程序,能不能體現一下C語言的實際用途,今天我們就理論結合實踐一把:C語言結合VBS腳本編寫一個簡單的朗讀小工具,做一個能夠發音的…

WAS集群系列(5):集群搭建:步驟3:安裝IHS軟件

選擇“安裝IBM HTTPServer”選項,點擊“安裝向導”。例如以下圖提示: 安裝提示,逐步點擊“下一步”,當中偶有幾處細節注意就可以。列舉例如以下: (1)、產品安裝路徑與先前WAS軟件所安裝路徑統一…

L1-056 猜數字 C語言,PAT L1-032 Left-pad

根據新浪微博上的消息,有一位開發者不滿NPM(Node Package Manager)的做法,收回了自己的開源代碼,其中包括一個叫left-pad的模塊,就是這個模塊把javascript里面的React/Babel干癱瘓了。這是個什么樣的模塊?就是在字符串…

單鏈表刪除整表_單鏈表刪除

單鏈表刪除整表Deletion can be at various positions like: 刪除可以在各個位置進行,例如: Deleting the first node 刪除第一個節點 Deleting the last node 刪除最后一個節點 Deleting the intermediate node 刪除中間節點 刪除單個鏈表中的第一個節…

WordPress數據表wp-options數據字段存JSON數據

2019獨角獸企業重金招聘Python工程師標準>>> wp_options表是WordPress中最重要的表,一切程序設置、主題設置和絕大多數插件的設置大都保存在此表。 WordPress里面數據表wp-options數據字段存JSON數據 : a:90: {s:11:"^wp-json/?$"…