_.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 AdcOutput
輸出量
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 AdcOutput
輸出量
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中移除元素的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: ai建立使用图案_ai自定义图案的方法详
- 下一篇: scala中字符串计数_如何在Scala