将数组按随机顺序拆分为多个数组 - Ruby

时间:2021-03-23 12:18:36

I am trying to split an array of names into multiple arrays in random order each time it is run. I know how to split them with:

我试图在每次运行时以随机顺序将一个名称数组拆分成多个数组。我知道如何拆分它们:

 name_array = ["bob","john","rob","nate","nelly","michael"]
 array = name_array.each_slice(2).to_a
 => [["bob", "john"], ["rob", "nate"], ["nelly", "michael"]]

But, what if I want it to spit them out in random order each time?

但是,如果我希望它每次都以随机顺序吐出来怎么办?

4 个解决方案

#1


5  

Before do the same thing, shuffle the array. (Array#shuffle)

在做同样的事情之前,将数组洗牌。 (阵列#洗牌)

name_array.shuffle.each_slice(2).to_a
# => [["nelly", "nate"], ["rob", "bob"], ["michael", "john"]]

or, shuffle afterward according to your need:

或者,根据您的需要随后洗牌:

name_array.each_slice(2).to_a.shuffle
# => [["nelly", "michael"], ["rob", "nate"], ["bob", "john"]]

#2


3  

Random Sampling

If you know the size of the original array, then:

如果您知道原始数组的大小,那么:

name_array.sample 6

Array#sample shortens your method chain over the use of Array#shuffle. If you don't want to hard-code the size of the sample, you can introspect the array for its size at run-time. For example:

Array#sample通过使用Array #shuffle缩短了方法链。如果您不想对示例的大小进行硬编码,则可以在运行时对数组的大小进行内省。例如:

name_array.sample(name_array.size)

Permutation

If you don't need to insist that a given name appears only once in your result set, then you might also consider Array#permutation:

如果您不需要坚持在结果集中只出现一次给定名称,那么您也可以考虑使用Array#permutation:

name_array.permutation(2).to_a.sample(name_array.size)

The results will vary, but here's a pretty-printed sample of the results you might expect from this approach:

结果会有所不同,但这里有一个漂亮的打印样本,您可以从这种方法中获得结果:

[["michael", "rob"],
 ["bob", "nelly"],
 ["rob", "bob"],
 ["michael", "nelly"],
 ["john", "michael"],
 ["john", "rob"]]

#3


2  

Shuffle the array first, then slice:

先将数组洗牌,然后切片:

["bob","john","rob","nate","nelly","michael"].shuffle.each_slice(2).to_a
#=> [["nelly", "nate"], ["rob", "michael"], ["john", "bob"]]

#4


2  

array = name_array.each_slice(2).to_a.shuffle

This will give you different results every time.

每次都会给你不同的结果。

#1


5  

Before do the same thing, shuffle the array. (Array#shuffle)

在做同样的事情之前,将数组洗牌。 (阵列#洗牌)

name_array.shuffle.each_slice(2).to_a
# => [["nelly", "nate"], ["rob", "bob"], ["michael", "john"]]

or, shuffle afterward according to your need:

或者,根据您的需要随后洗牌:

name_array.each_slice(2).to_a.shuffle
# => [["nelly", "michael"], ["rob", "nate"], ["bob", "john"]]

#2


3  

Random Sampling

If you know the size of the original array, then:

如果您知道原始数组的大小,那么:

name_array.sample 6

Array#sample shortens your method chain over the use of Array#shuffle. If you don't want to hard-code the size of the sample, you can introspect the array for its size at run-time. For example:

Array#sample通过使用Array #shuffle缩短了方法链。如果您不想对示例的大小进行硬编码,则可以在运行时对数组的大小进行内省。例如:

name_array.sample(name_array.size)

Permutation

If you don't need to insist that a given name appears only once in your result set, then you might also consider Array#permutation:

如果您不需要坚持在结果集中只出现一次给定名称,那么您也可以考虑使用Array#permutation:

name_array.permutation(2).to_a.sample(name_array.size)

The results will vary, but here's a pretty-printed sample of the results you might expect from this approach:

结果会有所不同,但这里有一个漂亮的打印样本,您可以从这种方法中获得结果:

[["michael", "rob"],
 ["bob", "nelly"],
 ["rob", "bob"],
 ["michael", "nelly"],
 ["john", "michael"],
 ["john", "rob"]]

#3


2  

Shuffle the array first, then slice:

先将数组洗牌,然后切片:

["bob","john","rob","nate","nelly","michael"].shuffle.each_slice(2).to_a
#=> [["nelly", "nate"], ["rob", "michael"], ["john", "bob"]]

#4


2  

array = name_array.each_slice(2).to_a.shuffle

This will give you different results every time.

每次都会给你不同的结果。