在Ruby中合并和插入两个数组。

时间:2022-07-26 12:19:54

I have the following code:

我有以下代码:

a = ["Cat", "Dog", "Mouse"]
s = ["and", "&"]

I want to merge the array s into array a which would give me:

我想把数组s合并到数组a中这样就能得到:

["Cat", "and", "Dog", "&", "Mouse"]

Looking through the Ruby Array and Enumerable docs, I don't see such a method that will accomplish this.

通过查看Ruby数组和可枚举文档,我没有看到这样的方法可以实现这一点。

Is there a way I can do this without iterating through each array?

有没有一种不用遍历每个数组就能做到的方法?

9 个解决方案

#1


163  

You can do that with:

你可以这样做:

a.zip(s).flatten.compact

#2


33  

This won't give a result array in the order Chris asked for, but if the order of the resulting array doesn't matter, you can just use a |= b. If you don't want to mutate a, you can write a | b and assign the result to a variable.

这不会按照Chris要求的顺序给出结果数组,但如果结果数组的顺序无关紧要,可以使用|= b。

See the set union documentation for the Array class at http://www.ruby-doc.org/core/classes/Array.html#M000275.

请参见http://www.ruby-doc.org/core/classes/Array.html#M000275中的数组联合文档。

This answer assumes that you don't want duplicate array elements. If you want to allow duplicate elements in your final array, a += b should do the trick. Again, if you don't want to mutate a, use a + b and assign the result to a variable.

这个答案假定您不需要重复的数组元素。如果您想在最后的数组中允许重复元素,那么a += b应该可以。同样,如果您不想更改a,则使用a + b,并将结果赋给一个变量。

In response to some of the comments on this page, these two solutions will work with arrays of any size.

为了响应本页上的一些评论,这两个解决方案将使用任意大小的数组。

#3


29  

If you don't want duplicate, why not just use the union operator :

如果你不想复制,为什么不直接使用union操作符:

new_array = a | s

#4


7  

s.inject(a, :<<)

s   #=> ["and", "&"]
a   #=> ["Cat", "Dog", "Mouse", "and", "&"]

It doesn't give you the order you asked for, but it's a nice way of merging two arrays by appending to the one.

它没有给出你想要的顺序,但是它是一种很好的方式,通过附加到一个数组来合并两个数组。

#5


6  

Here's a solution that allows interleaving multiple arrays of different sizes (general solution):

这里有一个解决方案,允许不同大小的多个数组交叉(通用解决方案):

arr = [["Cat", "Dog", "Mouse", "boo", "zoo"],
 ["and", "&"],
 ["hello", "there", "you"]]

first, *rest = *arr; first.zip(*rest).flatten.compact
=> ["Cat", "and", "hello", "Dog", "&", "there", "Mouse", "you", "boo", "zoo"]

#6


5  

It's not exactly elegant, but it works for arrays of any size:

它不是很优雅,但它适用于任何大小的数组:

>> a.map.with_index { |x, i| [x, i == a.size - 2 ? s.last : s.first] }.flatten[0..-2] 
#=> ["Cat", "and", "Dog", "&", "Mouse"]

#7


2  

How about a more general solution that works even if the first array isn't the longest and accepts any number of arrays?

如果有一种更通用的解决方案,即使第一个数组不是最长的,并且接受任意数量的数组,它也能工作?

a = [
    ["and", "&"],
    ["Cat", "Dog", "Mouse"]
]

b = a.max_by(&:length)
a -= [b]
b.zip(*a).flatten.compact

 => ["Cat", "and", "Dog", "&", "Mouse"]

#8


0  

One way to do the interleave and also guarantee which one is the biggest array for the zip method, is to fill up one of the arrays with nil until the other array size. This way, you also guarantee which element of which array will be on first position:

实现interleave的一种方法是用nil填充其中一个数组,直到另一个数组大小。这样,您还可以保证哪个元素的数组将位于第一个位置:

preferred_arr = ["Cat", "Dog", "Mouse"]
other_arr = ["and","&","are","great","friends"]

preferred_arr << nil while preferred_arr.length < other_arr.length
preferred_arr.zip(other_arr).flatten.compact
#=> ["Cat", "and", "Dog", "&", "Mouse", "are", "great", "friends"]

#9


-1  

arr = [0, 1]
arr + [2, 3, 4]

//outputs [0, 1, 2, 3, 4]

#1


163  

You can do that with:

你可以这样做:

a.zip(s).flatten.compact

#2


33  

This won't give a result array in the order Chris asked for, but if the order of the resulting array doesn't matter, you can just use a |= b. If you don't want to mutate a, you can write a | b and assign the result to a variable.

这不会按照Chris要求的顺序给出结果数组,但如果结果数组的顺序无关紧要,可以使用|= b。

See the set union documentation for the Array class at http://www.ruby-doc.org/core/classes/Array.html#M000275.

请参见http://www.ruby-doc.org/core/classes/Array.html#M000275中的数组联合文档。

This answer assumes that you don't want duplicate array elements. If you want to allow duplicate elements in your final array, a += b should do the trick. Again, if you don't want to mutate a, use a + b and assign the result to a variable.

这个答案假定您不需要重复的数组元素。如果您想在最后的数组中允许重复元素,那么a += b应该可以。同样,如果您不想更改a,则使用a + b,并将结果赋给一个变量。

In response to some of the comments on this page, these two solutions will work with arrays of any size.

为了响应本页上的一些评论,这两个解决方案将使用任意大小的数组。

#3


29  

If you don't want duplicate, why not just use the union operator :

如果你不想复制,为什么不直接使用union操作符:

new_array = a | s

#4


7  

s.inject(a, :<<)

s   #=> ["and", "&"]
a   #=> ["Cat", "Dog", "Mouse", "and", "&"]

It doesn't give you the order you asked for, but it's a nice way of merging two arrays by appending to the one.

它没有给出你想要的顺序,但是它是一种很好的方式,通过附加到一个数组来合并两个数组。

#5


6  

Here's a solution that allows interleaving multiple arrays of different sizes (general solution):

这里有一个解决方案,允许不同大小的多个数组交叉(通用解决方案):

arr = [["Cat", "Dog", "Mouse", "boo", "zoo"],
 ["and", "&"],
 ["hello", "there", "you"]]

first, *rest = *arr; first.zip(*rest).flatten.compact
=> ["Cat", "and", "hello", "Dog", "&", "there", "Mouse", "you", "boo", "zoo"]

#6


5  

It's not exactly elegant, but it works for arrays of any size:

它不是很优雅,但它适用于任何大小的数组:

>> a.map.with_index { |x, i| [x, i == a.size - 2 ? s.last : s.first] }.flatten[0..-2] 
#=> ["Cat", "and", "Dog", "&", "Mouse"]

#7


2  

How about a more general solution that works even if the first array isn't the longest and accepts any number of arrays?

如果有一种更通用的解决方案,即使第一个数组不是最长的,并且接受任意数量的数组,它也能工作?

a = [
    ["and", "&"],
    ["Cat", "Dog", "Mouse"]
]

b = a.max_by(&:length)
a -= [b]
b.zip(*a).flatten.compact

 => ["Cat", "and", "Dog", "&", "Mouse"]

#8


0  

One way to do the interleave and also guarantee which one is the biggest array for the zip method, is to fill up one of the arrays with nil until the other array size. This way, you also guarantee which element of which array will be on first position:

实现interleave的一种方法是用nil填充其中一个数组,直到另一个数组大小。这样,您还可以保证哪个元素的数组将位于第一个位置:

preferred_arr = ["Cat", "Dog", "Mouse"]
other_arr = ["and","&","are","great","friends"]

preferred_arr << nil while preferred_arr.length < other_arr.length
preferred_arr.zip(other_arr).flatten.compact
#=> ["Cat", "and", "Dog", "&", "Mouse", "are", "great", "friends"]

#9


-1  

arr = [0, 1]
arr + [2, 3, 4]

//outputs [0, 1, 2, 3, 4]