如何在Ruby中混洗数组/哈希?

时间:2022-03-10 12:18:29

For learning purposes, what is this called? Is the object being created an array or a hash?

出于学习目的,这叫什么?是创建对象是数组还是哈希?

stack_of_cards = []

This is how I'm filling it:

这就是我填写它的方式:

stack_of_cards << Card.new("A", "Spades", 1)
stack_of_cards << Card.new("2", "Spades", 2)
stack_of_cards << Card.new("3", "Spades", 3)
...

Here is my Card class:

这是我的卡类:

class Card

  attr_accessor :number, :suit, :value

  def initialize(number, suit, value)
    @number = number
    @suit = suit
    @value = value
  end

  def to_s
    "#{@number} of #{@suit}"
  end
end

I'd like to shuffle the elements in this array/hash (what is this called? :S)

我想改组这个数组/哈希中的元素(这叫做什么?:S)

Any suggestions?

6 个解决方案

#1


18  

stack_of_cards.shuffle

It is an Array, see http://www.ruby-doc.org/core-1.8.7/classes/Array.html for more information.

它是一个数组,有关详细信息,请参阅http://www.ruby-doc.org/core-1.8.7/classes/Array.html。

I've written the functional form, which returns a new Array, and it's the new one that's shuffled. You can instead use:

我已经编写了函数形式,它返回一个新的数组,并且它是新的数据,它被洗牌了。您可以改为使用:

stack_of_cards.shuffle!

...to shuffle the array in-place.

......将阵列原位洗牌。

#2


9  

If you want to shuffle a hash you can use something like this:

如果你想要改组哈希,你可以使用这样的东西:

class Hash
  def shuffle
    Hash[self.to_a.sample(self.length)]
  end

  def shuffle!
    self.replace(self.shuffle)
  end
end

I've posted this answer since I always find this question if I search for "ruby shuffle hash".

我发布了这个答案,因为如果我搜索“ruby shuffle hash”,我总能找到这个问题。

#3


1  

In addition to using the shuffle method, you can use the sort method:

除了使用shuffle方法,您还可以使用sort方法:

array.sort {|a, b| rand <=> rand }

This may be of use if you are using an older version of Ruby where shuffle is not implemented. As with shuffle!, you can use sort! to work on the existing array.

如果您使用的是较旧版本的Ruby,并且未实现shuffle,则可能会有用。和shuffle一样!你可以使用排序!处理现有的数组。

#4


1  

If you wanted to get crazy and write your own in-place shuffle method, you could do something like this.

如果你想变得疯狂并编写自己的就地shuffle方法,你可以做这样的事情。

 def shuffle_me(array)
   (array.size-1).downto(1) do |i|
     j = rand(i+1)
     array[i], array[j] = array[j], array[i]
   end

   array
 end 

#5


1  

For arrays:

array.shuffle
[1, 3, 2].shuffle
#=> [3, 1, 2]

For hashes:

Hash[*hash.to_a.shuffle.flatten]
Hash[*{a: 1, b: 2, c: 3}.to_a.shuffle.flatten(1)]
#=> {:b=>2, :c=>3, :a=>1}
#=> {:c=>3, :a=>1, :b=>2}
#=> {:a=>1, :b=>2, :c=>3}
# Also works for hashes containing arrays
Hash[*{a: [1, 2], b: [2, 3], c: [3, 4]}.to_a.shuffle.flatten(1)]
#=> {:b=>2, :c=>3, :a=>1}
#=> {:c=>[3, 4], :a=>[1, 2], :b=>[2, 3]}

#6


0  

If you want to shuffle a hash, but don't want to overload the Hash class, you can use the sort function and then convert it back to a hash with the to_h function (Ruby 2.1+):

如果你想改组哈希,但又不想重载哈希类,你可以使用sort函数,然后使用to_h函数将其转换回哈希值(Ruby 2.1+):

a = {"a" => 1, "b" => 2, "c" => 3}
puts a.inspect
a = a.sort {|a, b| rand <=> rand }.to_h
puts a.inspect

#1


18  

stack_of_cards.shuffle

It is an Array, see http://www.ruby-doc.org/core-1.8.7/classes/Array.html for more information.

它是一个数组,有关详细信息,请参阅http://www.ruby-doc.org/core-1.8.7/classes/Array.html。

I've written the functional form, which returns a new Array, and it's the new one that's shuffled. You can instead use:

我已经编写了函数形式,它返回一个新的数组,并且它是新的数据,它被洗牌了。您可以改为使用:

stack_of_cards.shuffle!

...to shuffle the array in-place.

......将阵列原位洗牌。

#2


9  

If you want to shuffle a hash you can use something like this:

如果你想要改组哈希,你可以使用这样的东西:

class Hash
  def shuffle
    Hash[self.to_a.sample(self.length)]
  end

  def shuffle!
    self.replace(self.shuffle)
  end
end

I've posted this answer since I always find this question if I search for "ruby shuffle hash".

我发布了这个答案,因为如果我搜索“ruby shuffle hash”,我总能找到这个问题。

#3


1  

In addition to using the shuffle method, you can use the sort method:

除了使用shuffle方法,您还可以使用sort方法:

array.sort {|a, b| rand <=> rand }

This may be of use if you are using an older version of Ruby where shuffle is not implemented. As with shuffle!, you can use sort! to work on the existing array.

如果您使用的是较旧版本的Ruby,并且未实现shuffle,则可能会有用。和shuffle一样!你可以使用排序!处理现有的数组。

#4


1  

If you wanted to get crazy and write your own in-place shuffle method, you could do something like this.

如果你想变得疯狂并编写自己的就地shuffle方法,你可以做这样的事情。

 def shuffle_me(array)
   (array.size-1).downto(1) do |i|
     j = rand(i+1)
     array[i], array[j] = array[j], array[i]
   end

   array
 end 

#5


1  

For arrays:

array.shuffle
[1, 3, 2].shuffle
#=> [3, 1, 2]

For hashes:

Hash[*hash.to_a.shuffle.flatten]
Hash[*{a: 1, b: 2, c: 3}.to_a.shuffle.flatten(1)]
#=> {:b=>2, :c=>3, :a=>1}
#=> {:c=>3, :a=>1, :b=>2}
#=> {:a=>1, :b=>2, :c=>3}
# Also works for hashes containing arrays
Hash[*{a: [1, 2], b: [2, 3], c: [3, 4]}.to_a.shuffle.flatten(1)]
#=> {:b=>2, :c=>3, :a=>1}
#=> {:c=>[3, 4], :a=>[1, 2], :b=>[2, 3]}

#6


0  

If you want to shuffle a hash, but don't want to overload the Hash class, you can use the sort function and then convert it back to a hash with the to_h function (Ruby 2.1+):

如果你想改组哈希,但又不想重载哈希类,你可以使用sort函数,然后使用to_h函数将其转换回哈希值(Ruby 2.1+):

a = {"a" => 1, "b" => 2, "c" => 3}
puts a.inspect
a = a.sort {|a, b| rand <=> rand }.to_h
puts a.inspect