日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

_.uniq_在Ruby中使用Array.compact和Array.uniq方法从Array中移除元素

發布時間:2025/3/11 编程问答 19 豆豆
生活随笔 收集整理的這篇文章主要介紹了 _.uniq_在Ruby中使用Array.compact和Array.uniq方法从Array中移除元素 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

_.uniq

Ruby Array.compact和Array.uniq方法 (Ruby Array.compact and Array.uniq Methods)

In the last article, we have gone through two different methods of deleting elements from the Array. We have seen their implementation with the help of their syntaxes and supporting examples. For a quick recall, let me tell those two methods were Array.delete() and Array.delete_at(). Array.delete() is a method that requires the name of the element which is supposed to be passed in the argument list of the Array. It works in a way that it deletes all the elements of the same name as mentioned in the argument list of Array.delete() whereas Array.delete_at() requires the index of the element we want to remove from the Array.

在上一篇文章中,我們介紹了從Array刪除元素的兩種不同方法 。 我們已經在它們的語法和支持示例的幫助下看到了它們的實現。 為了快速回顧一下,讓我說說這兩個方法是Array.delete()和Array.delete_at() 。 Array.delete()是一種方法,它需要假定應該在Array的參數列表中傳遞的元素的名稱。 它的工作方式是刪除與Array.delete()的參數列表中提到的名稱相同的所有元素,而Array.delete_at()則需要我們要從Array中刪除的元素的索引。

In the article, we will learn about two more and important methods of deleting the elements from the instance of Array class namely Array.comp() and Array.uniq()!.

在本文中,我們將學習另外兩個重要的刪除Array類實例中元素的方法,即Array.comp()和Array.uniq()! 。

Now let us go through their implementation with the help of their syntaxes and examples which you will find in the rest of the article.

現在,讓我們在它們的語法和示例的幫助下完成它們的實現,您將在本文的其余部分中找到它們。

1)Array.compact方法 (1) Array.compact Method)

Parameter(s):

參數:

This method requires no arguments.

此方法不需要任何參數。

Syntax:

句法:

Array.compact# orArray.compact!

Method description:

方法說明:

In most of the cases, you will find that your array is containing some nil values and you will be requiring some mechanism or lines of code to delete those nil values which are present in your Array. Ruby decreases your overhead of writing code for the same purpose by providing you a method named Array.compact. This method is used to remove all the nil values from the object of the Array class. Array.compact! is used to make changes in the Array as well.

在大多數情況下,您會發現數組中包含一些nil值,并且需要某種機制或代碼行來刪除數組中存在的這些nil值。 Ruby通過為您提供一個名為Array.compact的方法來減少出于相同目的而編寫代碼的開銷。 此方法用于從Array類的對象中刪除所有nil值。 Array.compact! 也用于在Array中進行更改。

Example:

例:

=beginRuby program to remove elements from Array using Array.compact =end# array declaration Adc = [nil,'Includehelp.com','Ruby','C++','C#','Java','Python','C++',nil,nil]# Array.compact method puts "Array.compact!" Adc.compact# printing array elements puts "Array elements are:" print Adc puts ""# Array.compact! method puts "Array.compact!" Adc.compact!# printing array elements puts "Array elements are:" print Adc

Output

輸出量

Array.compact! Array elements are: [nil, "Includehelp.com", "Ruby", "C++", "C#", "Java", "Python", "C++", nil, nil] Array.compact! Array elements are: ["Includehelp.com", "Ruby", "C++", "C#", "Java", "Python", "C++"]

Explanation:

說明:

In the above code, you can observe that Array.compact can be used to remove nil elements from the Array whereas Array.compact! can be used to make changes in the same Array as well.

在上面的代碼中,您可以觀察到Array.compact可用于從Array中刪除nil個元素,而Array.compact! 也可以用于在同一Array中進行更改。

2)Array.uniq方法 (2) Array.uniq Method)

Parameter (s):

參數:

No arguments required.

無需參數。

Syntax:

句法:

Array.uniq# orArray.uniq!

Method description:

方法說明:

Array.uniq or Array.uniq! can be used to remove duplicate elements from the Array. Sometimes you may observe that your Array is containing duplicate entries which may create any kind of complexity. Array.uniq is non-destructive whereas Array.uniq! is destructive, which means that the latter will create changes in the same Array as well.

Array.uniq或Array.uniq! 可用于從數組中刪除重復的元素。 有時,您可能會發現您的數組包含重復的條目,這可能會造成任何形式的復雜性。 Array.uniq是非破壞性的,而Array.uniq! 是破壞性的,這意味著后者也會在同一Array中創建更改。

Example:

例:

=beginRuby program to remove elements from Array using Array.uniq =end# array declaration Adc = ['Ruby','Includehelp.com','Ruby','C++','C#','Java','Python','C++','Java']# Array.uniq Method puts "Array.uniq" print Adc.uniq puts "" puts "Array elements are:" print Adc puts ""# Array.uniq! Method puts "Array.uniq!" Adc.uniq! puts "Array elements are:" print Adc

Output

輸出量

Array.uniq ["Ruby", "Includehelp.com", "C++", "C#", "Java", "Python"] Array elements are: ["Ruby", "Includehelp.com", "Ruby", "C++", "C#", "Java", "Python", "C++", "Java"] Array.uniq! Array elements are: ["Ruby", "Includehelp.com", "C++", "C#", "Java", "Python"]

Explanation:

說明:

In the above code, you can see that Array.uniq is not creating any change in the Array whereas it is possible with the help of Array.uniq! method. You can go for Sets if you want to avoid duplicacy.

在上面的代碼中,您可以看到Array.uniq并沒有在Array中創建任何更改,而借助Array.uniq可以實現! 方法。 如果要避免重復,可以選擇Sets。

翻譯自: https://www.includehelp.com/ruby/removing-elements-from-the-array-using-of-array-compact-and-array-uniq-methods.aspx

_.uniq

總結

以上是生活随笔為你收集整理的_.uniq_在Ruby中使用Array.compact和Array.uniq方法从Array中移除元素的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。

主站蜘蛛池模板: 国产精品麻豆成人av电影艾秋 | 国产黄色激情视频 | 91精品综合久久久久久 | 国产精品一 | 一区二区三区日韩在线 | 午夜免费一级片 | 伊人国产在线观看 | 国产精品无码一区 | 色欧美片视频在线观看 | 亚洲欧洲精品一区 | 日韩视频一| 国产乱国产乱老熟300部视频 | 精品熟妇无码av免费久久 | 日本三级免费 | 越南少妇做受xxx片 亚洲av综合色区无码一二三区 | 国产素人在线观看 | 黄色欧美在线观看 | 五月天激情丁香 | 日韩av在线观看免费 | 老司机精品视频网站 | 各种含道具高h调教1v1男男 | 扒下小娇妻的内裤打屁股 | 欧美激情在线一区二区 | 性涩av| 免费美女毛片 | 无码精品一区二区三区在线 | 亚洲videos | 久久久久人妻一区 | 美女免费av| 99性趣网 | 男人天堂网址 | 国产成人精品一区二区三区 | 欧美伊人| 天天夜夜啦啦啦 | 国产精品久久在线 | 黄色免费片 | 久久的色偷偷 | 国产精久久一区二区三区 | 午夜色福利 | chinese xxxx videos andvr | 超碰国产一区二区三区 | 午夜亚洲av永久无码精品 | 亚洲经典在线 | 婷婷综合久久 | 91亚洲精品久久久久久久久久久久 | 激情爱爱网站 | 久操网在线 | 伊人色爱 | 911精品| 天天舔天天插 | 国产偷v| 毛片视频软件 | 国产3区 | 波多野结av衣东京热无码专区 | 91精选视频 | 国产精品免费一区 | 日韩综合在线观看 | 亚洲av无码一区二区三区四区 | 亚洲精品成人片在线观看精品字幕 | 日本美女毛茸茸 | 亚洲免费黄色 | 波多野吉衣av在线 | 91色拍 | 性欧美大战久久久久久久 | 久久久久久久久久久久久女过产乱 | 亚洲久久久久久 | 一本色道久久综合狠狠躁的推荐 | 亚洲综合激情在线 | 91蜜桃视频| 看毛片的网址 | 日韩日b视频 | 国产二区三区视频 | 夜夜操av | 暗呦丨小u女国产精品 | 男男在线观看 | 亚洲精品人妻无码 | 亚洲黄色小说图片 | 三级免费网址 | 久久免 | 亚洲av无码乱码在线观看富二代 | 日日夜夜爱爱 | 美女极度色诱图片www视频 | 成人av片免费看 | 成人久久久精品乱码一区二区三区 | 九色.com| 久久久久黄色 | 国产成人福利视频 | 自拍偷拍欧美 | 极品少妇xxxx精品少妇 | 国产 丝袜 欧美中文 另类 | 国产片一区二区 | 在线黄色免费网站 | 国产亚洲精品成人av在线 | 国产成人午夜精品无码区久久 | 亚洲人成高清 | 日本成人福利视频 | 欧美日韩亚洲精品一区二区 | 九九热在线免费观看 | 久操超碰 |