_.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
# or
Array.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:

例:

=begin
Ruby 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
# or
Array.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.uniqArray.uniq! 可用于从数组中删除重复的元素。 有时,您可能会发现您的数组包含重复的条目,这可能会造成任何形式的复杂性。 Array.uniq是非破坏性的,而Array.uniq! 是破坏性的,这意味着后者也会在同一Array中创建更改。

Example:

例:

=begin
Ruby 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

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/543677.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

清除缓存的实现

现有的清除缓存存在着一些问题,占坑查询,后续进行完善转载于:https://www.cnblogs.com/lazVy/p/5192244.html

divi模板下载_Java Math类静态double IEEEremainder(double divi,double divisor)的示例

divi模板下载数学类静态double IEEEremainder(double divi,double divisor) (Math Class static double IEEEremainder(double divi , double divisor)) This method is available in java.lang package. 此方法在java.lang包中可用。 This method is used to retur…

nextdate函数白盒测试问题 软件测试_软件测试基本常识

一、软件测试的分类:1.按照是否执行被测试软件来分:静态测试:是指不运行软件,测试包括代码检查、静态结构分析、代码质量度量等,主要对软件需求说明书、设计说明书、软件源代码进行检查与分析。 动态测试:…

java 大小写转换函数_不使用Java中的任何库函数将大写转换为小写

java 大小写转换函数Given a string and we have to convert it from uppercase to lowercase. 给定一个字符串,我们必须将其从大写转换为小写。 Examples: 例子: Input:IncludeHelp.comOutput:includehelp.comInput:[email protected]Output:[email p…

ideatomcat老是运行以前的项目_日“吞”150吨垃圾,禅城集中式餐厨垃圾处理项目启用...

12月9日,禅城区集中式餐厨垃圾处理项目正式投料试运行,该项目如今每天可处理150吨垃圾。这意味着禅城区将通过先进技术实现餐厨垃圾资源化、减量化、无害化处理。禅城区集中式餐厨垃圾处理项目位于佛山市南庄污水处理厂首期工程北侧,禅港路西…

【HDOJ】4363 Draw and paint

看题解解的。将着色方案映射为40*40*5*5*5*5*2个状态&#xff0c;40*40表示n*m,5*5*5*5表示上下左右相邻块的颜色&#xff0c;0表示未着色。2表示横切或者竖切。基本思路是记忆化搜索然后去重&#xff0c;关键点是可能未切前当前块已经着色了。 1 /* 4363 */2 #include <ios…

java怎么知道上传文件是否成功_文件包含漏洞之——tomcat CVE-2020-1938漏洞复现

这个漏洞是今年2月份出现的&#xff0c;他的影响范围也是非常广的。2月20日&#xff0c;国家信息安全漏洞共享平台&#xff08;CNVD&#xff09;发布了Apache Tomcat文件包含漏洞&#xff08;CNVD-2020-10487/CVE-2020-1938&#xff09;&#xff0c;这个漏洞是由于Tomcat AJP协…

css word-wrap_CSS中分词“ break-all”和“ break-word”的值之间的差异

css word-wrapDefinition: 定义&#xff1a; What is the most fundamental element that comes to mind when you are considering to develop a web page? Words! If that was your answer, then pat yourself because you are already aware of what we are going to disc…

Android Studio apk 打包流程

1.Build -> Generate Signed APK...&#xff0c;打开如下窗口 2.假设这里没有打过apk包&#xff0c;点击Create new&#xff0c;窗口如下 这里只要输入几个必要项 Key store path&#xff08;生产key文件的保存路径 &#xff09; Key store password&#xff08;key 存储密码…

update yum 到指定版本_CentOS系统升级至指定版本

摘要&#xff1a;CentOS系统下用yum updates命令默认只能升级到最新版本&#xff0c;CentOS仓库并不维护历史版本&#xff0c;所以只能使用 vault.centos.org 历史版本快照进行更新。本文以...CentOS系统下用yum updates命令默认只能升级到最新版本&#xff0c;CentOS仓库并不维…

Redis过期key清理机制

Redis的key过期时间探讨 说到Redis是一个典型的key-value非关系型数据库&#xff0c;存储的key基本都有过期时间&#xff0c;或者有默认的过期时间&#xff0c;或者不设置的话永久不失效&#xff08;内存空间足够大的情况下&#xff0c;生产环境一般放置系统的配置参数才这样&a…

错误:使用printf()打印Hello world时未声明'Hello'/ Text

While printing "Hello world", if this error Hello undeclared occurred that means Hello is supplied to the compiler as a variable not as a text/string. 在打印“ Hello world”时 &#xff0c;如果发生未声明的错误“ Hello” &#xff0c;则意味着Hello是…

C#中毫米与像素的换算方法

C#中以像素作为尺寸单位&#xff0c;像素是一种相对的尺寸概念&#xff0c;与毫米的转换与当前显示器的分辨率有关。在不同分辨率下转换的系数不同。 借助GDI可以完成毫米至像素的转换。 public static double MillimetersToPixelsWidth(double length) //length是毫米&#xf…

Python | 使用__del __()和__init __()实现析构函数和构造函数的示例

To implement a constructor, we use __init()__ and to implement a destructor, we use __del()__ in python. 为了实现构造函数&#xff0c;我们使用__init()__ &#xff1b;为了实现析构函数&#xff0c;我们使用python中的__del()__ 。 Program: 程序&#xff1a; class…

drawer的用法_MMDrawerController抽屉侧边栏的简单使用

1.MMDrawerController是一个简单实用的侧边栏第三方类库。2.在appdelegate页中初始化你需要的左右侧边栏&#xff0c;leftViewController &#xff0c;mainViewController。3.在appdelegate中导入头文件#import "MMDrawerController.h"4.初始化抽屉控制器&#xff1a…

【Android】11.3 屏幕旋转和场景变换过程中GridView的呈现

分类&#xff1a;C#、Android、VS2015&#xff1b; 创建日期&#xff1a;2016-02-21 一、简介 实际上&#xff0c;对于布局文件中的View来说&#xff0c;大多数情况下&#xff0c;Android都会自动保存这些状态&#xff0c;并不需要我们都去处理它。这一节仍以GridView为例&…

foss测试_社区概念-与FOSS社区合作时应遵循的准则

foss测试A steady arrangement of strategies and rules is expected to administer FOSS inside an association. These strategies and rules must be deliberately created to protect that all issues that may influence the interests of the association are tended to.…

74ls161中rco是什么_74ls161引脚图与管脚功能表资料

输入输出CRCPLDEPETD3D2D1D0Q3Q2Q1Q00ФФФФФФФФ00001↑0ФФdcbadcba1↑10ФФФФФQ3Q2Q1Q01↑1Ф0ФФФФQ3Q2Q1Q01↑111ФФФФ状态码加1<74LS161功能表>从74LS161功能表功能表中可以知道&#xff0c;当清零端CR“0”&#xff0c;计数器输出Q3、Q2、Q1、Q0立…

html---textarea初始化时就有个table空格以及tab键操作无效

1 初始化时就有一个tab空格这是由于<textarea></textarea>之间的内容不为空的原因&#xff0c;包含空格和换行&#xff0c;否则浏览器会觉得空格或者换行都是文本域的内容。因此书写时需将<textarea></textarea>紧靠在一起。2 tab键对textarea操作无效…

如何使用JavaScript访问对象的键中有空格的对象?

Sometimes your JavaScript object may contain a key having spaces between them. As a key can also be a string and a string may contain spaces, it is very much possible that you encounter this problem. Consider the following object, 有时&#xff0c;您JavaScr…