如何删除Ruby中所有满足条件的元素?

时间:2021-03-28 07:57:14

How can I implement this in Ruby? Is there any one line of code technique? Let's say I want to get rid of all the elements which are less than 3 of an integer array.

我如何在Ruby中实现它?有任何一行代码技术吗?假设我要消去小于3个整数数组的所有元素。

4 个解决方案

#1


52  

You can use either new_array = array.reject {|x| x < 3} (reject returns a new array) or array.reject! {|x| x < 3} (reject! aka delete_if modifies the array in place).

可以使用new_array = array。拒绝{|x| x < 3}(拒绝返回一个新数组)或array.reject!{|x| x < 3}(拒绝!即delete_if修改数组)。

There's also the (somewhat more common) select method, which acts like reject except that you specify the condition to keep elements, not to reject them (i.e. to get rid of the elements less than 3, you'd use new_array = array.select {|x| x >= 3}).

还有一个(比较常见的)选择方法,它的作用类似于拒绝,除非您指定条件来保存元素,而不是拒绝它们(例如,为了去掉小于3的元素,您将使用new_array = array)。选择{|x| x >= 3})。

#2


9  

Probably worth pointing out that

也许值得指出。

array.reject! {|x| x < 3}

and

array.delete_if {|x| x < 3}

Are the same, but

是相同的,但

array.reject {|x| x < 3}

Will still return the same result, but not change the "array".

仍然返回相同的结果,但不更改“数组”。

#3


8  

  a = [ "a", "b", "c" ]
  a.delete_if {|x| x >= "b" }   #=> ["a"]

#4


0  

This works fine for numbers and letters in alphabetical order. their values are compared, what if the conditions change?

这适用于按字母顺序排列的数字和字母。他们的值被比较,如果条件改变怎么办?

array = ["Type", ": Jointed", "Axes", ": 6", "Reach", ": 951 mm", "Capacity", ": 6 Kg", "Uses", ": ", "Arc welding, material handling, machine loading, application", "This particular unit is in excellent condition with under 700 hours."]

We need to delete all elemetns after the "Uses" value example:

我们需要删除“使用”值示例后的所有元素:

array = ["Type", ": Jointed", "Axes", ": 6", "Reach", ": 951 mm", "Capacity", ": 6 Kg"]

So, that desition is not working (it just remove one element):

所以,这个设计没有效果(它只删除了一个元素):

array.delete_if {|x| x >= "Uses" }
["Type", ": Jointed", "Axes", ": 6", "Reach", ": 951 mm", "Capacity", ": 6 Kg", ": ", "Arc welding, material handling, machine loading, application", "This particular unit is in excellent condition with under 700 hours."]

#1


52  

You can use either new_array = array.reject {|x| x < 3} (reject returns a new array) or array.reject! {|x| x < 3} (reject! aka delete_if modifies the array in place).

可以使用new_array = array。拒绝{|x| x < 3}(拒绝返回一个新数组)或array.reject!{|x| x < 3}(拒绝!即delete_if修改数组)。

There's also the (somewhat more common) select method, which acts like reject except that you specify the condition to keep elements, not to reject them (i.e. to get rid of the elements less than 3, you'd use new_array = array.select {|x| x >= 3}).

还有一个(比较常见的)选择方法,它的作用类似于拒绝,除非您指定条件来保存元素,而不是拒绝它们(例如,为了去掉小于3的元素,您将使用new_array = array)。选择{|x| x >= 3})。

#2


9  

Probably worth pointing out that

也许值得指出。

array.reject! {|x| x < 3}

and

array.delete_if {|x| x < 3}

Are the same, but

是相同的,但

array.reject {|x| x < 3}

Will still return the same result, but not change the "array".

仍然返回相同的结果,但不更改“数组”。

#3


8  

  a = [ "a", "b", "c" ]
  a.delete_if {|x| x >= "b" }   #=> ["a"]

#4


0  

This works fine for numbers and letters in alphabetical order. their values are compared, what if the conditions change?

这适用于按字母顺序排列的数字和字母。他们的值被比较,如果条件改变怎么办?

array = ["Type", ": Jointed", "Axes", ": 6", "Reach", ": 951 mm", "Capacity", ": 6 Kg", "Uses", ": ", "Arc welding, material handling, machine loading, application", "This particular unit is in excellent condition with under 700 hours."]

We need to delete all elemetns after the "Uses" value example:

我们需要删除“使用”值示例后的所有元素:

array = ["Type", ": Jointed", "Axes", ": 6", "Reach", ": 951 mm", "Capacity", ": 6 Kg"]

So, that desition is not working (it just remove one element):

所以,这个设计没有效果(它只删除了一个元素):

array.delete_if {|x| x >= "Uses" }
["Type", ": Jointed", "Axes", ": 6", "Reach", ": 951 mm", "Capacity", ": 6 Kg", ": ", "Arc welding, material handling, machine loading, application", "This particular unit is in excellent condition with under 700 hours."]